<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN" class="translated-ltr"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>第11章数据类型</title>
<link rel="stylesheet" href="mvl.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets + chunker.py v1.9.2">
<link rel="start" href="index.html" title="{书名}">
<link rel="up" href="" title="">
<link rel="prev" href="charset.html" title="第10章字符集，排序规则，Unicode">
<link rel="next" href="functions.html" title="第12章函数和操作符">
 
<link type="text/css" rel="stylesheet" charset="UTF-8" href="https://translate.googleapis.com/translate_static/css/translateelement.css"></head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tbody><tr>
<th colspan="3" align="center"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11章数据类型</font></font></th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="charset.html"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">上一页</font></font></a>&nbsp;</td>
<th width="60%" align="center"></th>
<td width="20%" align="right">&nbsp;<a accesskey="n" href="functions.html"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">下一个</font></font></a></td>
</tr>
</tbody></table>
<hr>
</div>
<div class="chapter">
<div class="titlepage">
<div>
<div>
<h1 class="title"><a name="data-types"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11章数据类型</font></font></h1>

</div>

</div>

</div>
<div class="toc">
<p><b><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">目录</font></font></b></p><dl class="toc"><dt><span class="section"><a href="data-types.html#data-type-overview"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.1数据类型概述</font></font></a></span></dt><dd><dl><dt><span class="section"><a href="data-types.html#numeric-type-overview"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.1.1数字类型概述</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#date-and-time-type-overview"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11。1。2日期和时间类型概述</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#string-type-overview"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.1.3字符串类型概述</font></font></a></span></dt></dl></dd><dt><span class="section"><a href="data-types.html#numeric-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2数字类型</font></font></a></span></dt><dd><dl><dt><span class="section"><a href="data-types.html#integer-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#fixed-point-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.2定点类型（精确值） -  DECIMAL，NUMERIC</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#floating-point-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.3浮点类型（近似值） -  FLOAT，DOUBLE</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#bit-type"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.4比特值类型 -  BIT</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#numeric-type-attributes"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.5数字类型属性</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#out-of-range-and-overflow"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.6超出范围和溢出处理</font></font></a></span></dt></dl></dd><dt><span class="section"><a href="data-types.html#date-and-time-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11。3日期和时间类型</font></font></a></span></dt><dd><dl><dt><span class="section"><a href="data-types.html#datetime"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.3.1 DATE，DATETIME和TIMESTAMP类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#time"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.3.2 TIME类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#year"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.3.3年份类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#timestamp-initialization"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.3.4 TIMESTAMP和DATETIME的自动初始化和更新</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#fractional-seconds"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.3.5时间值的分数秒</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#date-and-time-type-conversion"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.3.6日期和时间类型之间的转换</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#two-digit-years"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11。3。2日期的两位数年份</font></font></a></span></dt></dl></dd><dt><span class="section"><a href="data-types.html#string-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.4字符串类型</font></font></a></span></dt><dd><dl><dt><span class="section"><a href="data-types.html#char"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.4.1 CHAR和VARCHAR类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#binary-varbinary"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.4.2 BINARY和VARBINARY类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#blob"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.4.3 BLOB和TEXT类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#enum"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.4.4 ENUM类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#set"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.4.5 SET类型</font></font></a></span></dt></dl></dd><dt><span class="section"><a href="data-types.html#spatial-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5空间数据类型</font></font></a></span></dt><dd><dl><dt><span class="section"><a href="data-types.html#spatial-type-overview"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.1空间数据类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#opengis-geometry-model"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.2 OpenGIS几何模型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#gis-data-formats"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.3支持的空间数据格式</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#geometry-well-formedness-validity"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.4几何形状良好和有效性</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#spatial-reference-systems"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.5空间参考系统支持</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#creating-spatial-columns"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.6创建空间列</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#populating-spatial-columns"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.7填充空间列</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#fetching-spatial-data"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.8获取空间数据</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#optimizing-spatial-analysis"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.9优化空间分析</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#creating-spatial-indexes"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.10创建空间索引</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#using-spatial-indexes"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.11使用空间索引</font></font></a></span></dt></dl></dd><dt><span class="section"><a href="data-types.html#json"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.6 JSON数据类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#data-type-defaults"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.7数据类型默认值</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#storage-requirements"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.8数据类型存储要求</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#choosing-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.9为列选择正确的类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#other-vendor-data-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.10使用其他数据库引擎中的数据类型</font></font></a></span></dt></dl>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    MySQL支持多种</font><font style="vertical-align: inherit;">类型</font><font style="vertical-align: inherit;">的</font></font><a class="link" href="glossary.html#glos_sql" title="SQL"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">SQL</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型：数字类型，日期和时间类型，字符串（字符和字节）类型，空间类型和
     </font></font><a class="link" href="data-types.html#json" title="11.6 JSON数据类型"><code class="literal">JSON</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型。</font><font style="vertical-align: inherit;">本章概述了这些数据类型，每个类别中类型属性的更详细描述，以及数据类型存储要求的摘要。</font><font style="vertical-align: inherit;">初步概述是故意简短的。</font><font style="vertical-align: inherit;">有关特定数据类型的其他信息（例如可以指定值的允许格式），请参阅本章后面的更详细说明。
  </font></font></p><a class="indexterm" name="idm140091694804336"></a><a class="indexterm" name="idm140091694803264"></a><a class="indexterm" name="idm140091694801776"></a><a class="indexterm" name="idm140091694800288"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    数据类型描述使用以下约定：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        对于整数类型，</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表示最大显示宽度。</font><font style="vertical-align: inherit;">对于浮点和定点类型，
         </font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是可以存储的总位数（精度）。</font><font style="vertical-align: inherit;">对于字符串类型，
         </font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是最大长度。</font><font style="vertical-align: inherit;">允许的最大值</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">取决于数据类型。
      </font></font></p><a class="indexterm" name="idm140091694795504"></a><a class="indexterm" name="idm140091694794432"></a><a class="indexterm" name="idm140091694792944"></a><a class="indexterm" name="idm140091694791872"></a><a class="indexterm" name="idm140091694790384"></a><a class="indexterm" name="idm140091694789312"></a></li><li class="listitem"><p>
        <a class="indexterm" name="idm140091694787184"></a>

        <a class="indexterm" name="idm140091694786112"></a>

        <a class="indexterm" name="idm140091694784624"></a>

        <a class="indexterm" name="idm140091694783552"></a>

        <em class="replaceable"><code>D</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">适用于浮点和定点类型，并指示小数点后面的位数（刻度）。</font><font style="vertical-align: inherit;">最大可能值为30，但不应大于
         </font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">-2。
      </font></font></p></li><li class="listitem"><p>
        <em class="replaceable"><code>fsp</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">适用于
         </font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型和表示小数精度秒; </font><font style="vertical-align: inherit;">也就是说，小数部分秒的小数点后面的位数。</font><font style="vertical-align: inherit;">的
         </font></font><em class="replaceable"><code>fsp</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值，如果给定的，必须在1到6的值为0表示没有小数部分范围为0。</font><font style="vertical-align: inherit;">如果省略，则默认精度为0.（这与标准SQL默认值6不同，以便与以前的MySQL版本兼容。）
      </font></font></p><a class="indexterm" name="idm140091694775520"></a><a class="indexterm" name="idm140091694774432"></a></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        方括号（</font></font><code class="literal">[</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">]</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）表示类型定义的可选部分。
</font></font></p><a class="indexterm" name="idm140091694770704"></a><a class="indexterm" name="idm140091694769216"></a></li></ul>
</div>

<div class="section">

<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="data-type-overview"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.1数据类型概述</font></font></h2>

</div>

</div>

</div>

<div class="toc">
<dl class="toc"><dt><span class="section"><a href="data-types.html#numeric-type-overview"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.1.1数字类型概述</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#date-and-time-type-overview"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11。1。2日期和时间类型概述</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#string-type-overview"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.1.3字符串类型概述</font></font></a></span></dt></dl>
</div>
<a class="indexterm" name="idm140091694766800"></a>
<div class="section">

<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="numeric-type-overview"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.1.1数字类型概述</font></font></h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091694764160"></a><a class="indexterm" name="idm140091694763088"></a><a class="indexterm" name="idm140091694762016"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        下面是数值数据类型的摘要。</font><font style="vertical-align: inherit;">有关数字类型的属性和存储要求的其他信息，请参见</font></font><a class="xref" href="data-types.html#numeric-types" title="11.2数字类型"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.2节“数值类型”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.8节“数据类型存储要求”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        对于整数类型，</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表示最大显示宽度。</font><font style="vertical-align: inherit;">最大显示宽度为255.显示宽度与类型可包含的值范围无关，如</font></font><a class="xref" href="data-types.html#numeric-types" title="11.2数字类型"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.2节“数值类型”中所述</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        对于浮点和定点类型，
         </font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是可以存储的总位数。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        从MySQL 8.0.17开始，对于整数数据类型，不推荐使用display width属性，并且将在以后的MySQL版本中删除它。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如果指定</font></font><code class="literal">ZEROFILL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数字列，MySQL会自动将该</font></font><code class="literal">UNSIGNED</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        属性</font><font style="vertical-align: inherit;">添加</font><font style="vertical-align: inherit;">到列中。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        从MySQL 8.0.17开始，该</font></font><code class="literal">ZEROFILL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性不推荐用于数字数据类型，并将在未来的MySQL版本中删除。</font><font style="vertical-align: inherit;">考虑使用另一种方法来产生该属性的效果。</font><font style="vertical-align: inherit;">例如，应用程序可以使用该</font></font><a class="link" href="functions.html#function_lpad"><code class="literal">LPAD()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        函数将数字填充到所需的宽度，或者它们可以将格式化的数字存储在
         </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列中。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        允许该</font></font><code class="literal">UNSIGNED</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        属性的</font><font style="vertical-align: inherit;">数字数据类型</font><font style="vertical-align: inherit;">也允许</font></font><code class="literal">SIGNED</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">但是，默认情况下</font></font><code class="literal">SIGNED</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">会对</font><font style="vertical-align: inherit;">这些数据类型进行签名，因此该
         </font><font style="vertical-align: inherit;">属性不起作用。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如MySQL的8.0.17中，</font></font><code class="literal">UNSIGNED</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性是不鼓励对类型的列
         </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（和任何同义词）和将在以后的MySQL版本被删除。</font><font style="vertical-align: inherit;">考虑使用简单</font></font><code class="literal">CHECK</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">约束代替此类列。
      </font></font></p><p>
        <code class="literal">SERIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是别名</font></font><code class="literal">BIGINT
        UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p>
        <code class="literal">SERIAL DEFAULT VALUE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在整数列的定义中是别名</font></font><code class="literal">NOT NULL AUTO_INCREMENT
        UNIQUE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
<div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
警告
</font></font></div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          当您在一个类型的整数值之间使用减法时</font></font><code class="literal">UNSIGNED</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，除非</font></font><a class="link" href="server-administration.html#sqlmode_no_unsigned_subtraction"><code class="literal">NO_UNSIGNED_SUBTRACTION</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">启用S​​QL模式，</font><font style="vertical-align: inherit;">否则结果是无符号的
           </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">请参见</font></font><a class="xref" href="functions.html#cast-functions" title="12.10演员函数和运算符"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第12.10节“强制转换函数和运算符”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
</div>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <a class="indexterm" name="idm140091694731824"></a>

            <a class="indexterm" name="idm140091694730752"></a>

            <a class="link" href="data-types.html#bit-type" title="11.2.4比特值类型 -  BIT"><code class="literal">BIT[(<em class="replaceable"><code>M</code></em>)]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            位值类型。</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表示每个值的位数，从1到64.如果</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">省略，</font><font style="vertical-align: inherit;">则默认值为1
             </font><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694722960"></a>

            <a class="indexterm" name="idm140091694721888"></a>

            <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">TINYINT[(<em class="replaceable"><code>M</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一个非常小的整数。</font><font style="vertical-align: inherit;">签署的范围是
             </font></font><code class="literal">-128</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到</font></font><code class="literal">127</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">无符号的范围是</font></font><code class="literal">0</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到
             </font></font><code class="literal">255</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694712176"></a>

            <a class="indexterm" name="idm140091694711104"></a>

            <a class="indexterm" name="idm140091694710032"></a>

            <a class="indexterm" name="idm140091694708544"></a>

            <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BOOL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
            </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BOOLEAN</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            这些类型是同义词
             </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">TINYINT(1)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">值为零被视为false。</font><font style="vertical-align: inherit;">非零值被认为是真的：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT IF(0, 'true', 'false');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">IF（0，'true'，'false'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">假|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------ +</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT IF(1, 'true', 'false');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">IF（1，'true'，'false'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">是的|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------ +</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT IF(2, 'true', 'false');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">IF（2，'true'，'false'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">是的|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------ +</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            然而，这些值</font></font><code class="literal">TRUE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
             </font></font><code class="literal">FALSE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">仅是别名为
             </font></font><code class="literal">1</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">0</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">分别，如下所示：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT IF(0 = FALSE, 'true', 'false');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">IF（0 = FALSE，'true'，'false'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">是的|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------------------------- +</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT IF(1 = TRUE, 'true', 'false');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">IF（1 = TRUE，'true'，'false'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">是的|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------- +</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT IF(2 = TRUE, 'true', 'false');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">IF（2 = TRUE，'true'，'false'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">假|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------- +</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT IF(2 = FALSE, 'true', 'false');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">IF（2 = FALSE，'true'，'false'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">假|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------------------------- +</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            最后两个语句显示显示的结果，因为
             </font></font><code class="literal">2</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">它们既不等
             </font></font><code class="literal">1</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">也不等</font></font><code class="literal">0</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694684816"></a>

            <a class="indexterm" name="idm140091694683744"></a>

            <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">SMALLINT[(<em class="replaceable"><code>M</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一个小整数。</font><font style="vertical-align: inherit;">签署的范围是
             </font></font><code class="literal">-32768</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到</font></font><code class="literal">32767</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">无符号的范围是</font></font><code class="literal">0</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到
             </font></font><code class="literal">65535</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694673968"></a>

            <a class="indexterm" name="idm140091694672896"></a>

            <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">MEDIUMINT[(<em class="replaceable"><code>M</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一个中等大小的整数。</font><font style="vertical-align: inherit;">签署的范围是
             </font></font><code class="literal">-8388608</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到</font></font><code class="literal">8388607</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">无符号的范围是</font></font><code class="literal">0</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到
             </font></font><code class="literal">16777215</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694663136"></a>

            <a class="indexterm" name="idm140091694662064"></a>

            <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT[(<em class="replaceable"><code>M</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            正常大小的整数。</font><font style="vertical-align: inherit;">签署的范围是
             </font></font><code class="literal">-2147483648</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到
             </font></font><code class="literal">2147483647</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">无符号的范围是
             </font></font><code class="literal">0</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到</font></font><code class="literal">4294967295</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694653024"></a>

            <a class="indexterm" name="idm140091694651952"></a>

            <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INTEGER[(<em class="replaceable"><code>M</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            此类型是其同义词
             </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694643840"></a>

            <a class="indexterm" name="idm140091694642768"></a>

            <a class="indexterm" name="idm140091694641280"></a>

            <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT[(<em class="replaceable"><code>M</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一个大整数。</font><font style="vertical-align: inherit;">签署的范围是
             </font></font><code class="literal">-9223372036854775808</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到
             </font></font><code class="literal">9223372036854775807</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">无符号的范围是</font></font><code class="literal">0</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到
             </font></font><code class="literal">18446744073709551615</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p><p>
            <code class="literal">SERIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是别名</font></font><code class="literal">BIGINT
            UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            关于</font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列，</font><font style="vertical-align: inherit;">您应该注意的一些事项
             </font><font style="vertical-align: inherit;">：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                <a class="indexterm" name="idm140091694630240"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

                所有算术都是使用有符号
                 </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
                 </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值完成的，因此</font></font><code class="literal">9223372036854775807</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">除了位函数之外，</font><font style="vertical-align: inherit;">不应使用大于</font><font style="vertical-align: inherit;">（63位）的</font><font style="vertical-align: inherit;">无符号大整数
                 </font><font style="vertical-align: inherit;">！</font><font style="vertical-align: inherit;">如果这样做，结果中的一些最后数字可能是错误的，因为在将</font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值</font><font style="vertical-align: inherit;">转换</font><font style="vertical-align: inherit;">为a
                 </font><font style="vertical-align: inherit;">时出现舍入错误
                 </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
              </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
                MySQL可以</font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
                在以下情况下</font><font style="vertical-align: inherit;">处理</font><font style="vertical-align: inherit;">：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: square; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
                    使用整数在</font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列中</font><font style="vertical-align: inherit;">存储大的无符号值时</font><font style="vertical-align: inherit;">。
                  </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
                    在
                     </font><font style="vertical-align: inherit;">
                    或中
                     </font><font style="vertical-align: inherit;">，where </font><font style="vertical-align: inherit;">指的是</font><font style="vertical-align: inherit;">列。
                  </font></font><a class="link" href="functions.html#function_min"><code class="literal">MIN(<em class="replaceable"><code>col_name</code></em>)</code></a><font style="vertical-align: inherit;"></font><a class="link" href="functions.html#function_max"><code class="literal">MAX(<em class="replaceable"><code>col_name</code></em>)</code></a><font style="vertical-align: inherit;"></font><em class="replaceable"><code>col_name</code></em><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a><font style="vertical-align: inherit;"></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
                    当使用操作符（</font></font><a class="link" href="functions.html#operator_plus"><code class="literal">+</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
                     </font></font><a class="link" href="functions.html#operator_minus"><code class="literal">-</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
                     </font></font><a class="link" href="functions.html#operator_times"><code class="literal">*</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，等等），其中两个操作数都是整数。
</font></font></p></li></ul>
</div>
</li><li class="listitem"><p><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">通过使用字符串存储它，</font><font style="vertical-align: inherit;">
                您始终可以在</font><font style="vertical-align: inherit;">列中存储</font><font style="vertical-align: inherit;">精确的整数值
                 </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">在这种情况下，MySQL执行字符串到数字的转换，不涉及中间双精度表示。
              </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
                的</font></font><a class="link" href="functions.html#operator_minus"><code class="literal">-</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
                 </font></font><a class="link" href="functions.html#operator_plus"><code class="literal">+</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
                 </font></font><a class="link" href="functions.html#operator_times"><code class="literal">*</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
                运营商使用</font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
                算术当两个操作数是整数值。</font><font style="vertical-align: inherit;">这意味着如果将两个大整数（或返回整数的函数的结果）相乘，则当结果大于时，可能会得到意外结果
                 </font></font><code class="literal">9223372036854775807</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p></li></ul>
</div>
</li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694596336"></a>

            <a class="indexterm" name="idm140091694595264"></a>

            <a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL[(<em class="replaceable"><code>M</code></em>[,<em class="replaceable"><code>D</code></em>])]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            打包的</font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">“ </font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">精确</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> ”</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">定点数。
            </font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是总位数（精度），</font></font><em class="replaceable"><code>D</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是小数点后的位数（刻度）。</font><font style="vertical-align: inherit;">小数点和（对于负数）
             </font></font><code class="literal">-</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">符号不计入
             </font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">如果
             </font></font><em class="replaceable"><code>D</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">为0，则值没有小数点或小数部分。</font><font style="vertical-align: inherit;">最大位数（</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）为
             </font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">65.最大支持小数数（</font></font><em class="replaceable"><code>D</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）为30.如果</font></font><em class="replaceable"><code>D</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">省略，则默认值为0.如果</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">省略，则默认值为10。
          </font></font></p><p>
            <code class="literal">UNSIGNED</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，如果指定，则禁止否定值。</font><font style="vertical-align: inherit;">从MySQL 8.0.17开始，</font></font><code class="literal">UNSIGNED</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">对于类型</font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（和任何同义词）的</font><font style="vertical-align: inherit;">列，不推荐使用</font><font style="vertical-align: inherit;">该</font><font style="vertical-align: inherit;">属性，</font><font style="vertical-align: inherit;">并且将在以后的MySQL版本中删除</font><font style="vertical-align: inherit;">该
             </font><font style="vertical-align: inherit;">属性</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">考虑使用简单</font></font><code class="literal">CHECK</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">约束代替此类列。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            所有</font></font><code class="literal">+, -, *, /</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">带</font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列的</font><font style="vertical-align: inherit;">基本计算（</font><font style="vertical-align: inherit;">）
             </font><font style="vertical-align: inherit;">都以65位的精度完成。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694575440"></a>

            <a class="indexterm" name="idm140091694574368"></a>

            <a class="indexterm" name="idm140091694573296"></a>

            <a class="indexterm" name="idm140091694572224"></a>

            <a class="indexterm" name="idm140091694570768"></a>

            <a class="indexterm" name="idm140091694569280"></a>

            <a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DEC[(<em class="replaceable"><code>M</code></em>[,<em class="replaceable"><code>D</code></em>])]
            [UNSIGNED] [ZEROFILL]</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
             </font><font style="vertical-align: inherit;">，
            </font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">NUMERIC[(<em class="replaceable"><code>M</code></em>[,<em class="replaceable"><code>D</code></em>])]
            [UNSIGNED] [ZEROFILL]</code></a><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">FIXED[(<em class="replaceable"><code>M</code></em>[,<em class="replaceable"><code>D</code></em>])]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            这些类型是同义词
             </font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">该
             </font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">FIXED</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">同义词可用于与其他数据库系统兼容。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694555344"></a>

            <a class="indexterm" name="idm140091694554272"></a>

            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT[(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p>
            A small (single-precision) floating-point number.
            Permissible values are <code class="literal">-3.402823466E+38</code>
            to <code class="literal">-1.175494351E-38</code>,
            <code class="literal">0</code>, and <code class="literal">1.175494351E-38</code>
            to <code class="literal">3.402823466E+38</code>. These are the
            theoretical limits, based on the IEEE standard. The actual
            range might be slightly smaller depending on your hardware
            or operating system.
          </p><p>
            <em class="replaceable"><code>M</code></em> is the total number of digits
            and <em class="replaceable"><code>D</code></em> is the number of digits
            following the decimal point. If <em class="replaceable"><code>M</code></em>
            and <em class="replaceable"><code>D</code></em> are omitted, values are
            stored to the limits permitted by the hardware. A
            single-precision floating-point number is accurate to
            approximately 7 decimal places.
          </p><p>
            <code class="literal">FLOAT(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code>
            is a nonstandard MySQL extension. As of MySQL 8.0.17, this
            syntax is deprecated and support for it will be removed in a
            future MySQL version.
          </p><p>
            <code class="literal">UNSIGNED</code>, if specified, disallows
            negative values. As of MySQL 8.0.17, the
            <code class="literal">UNSIGNED</code> attribute is deprecated for
            columns of type <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a> (and
            any synonyms) and will be removed in a future MySQL version.
            Consider using a simple <code class="literal">CHECK</code> constraint
            instead for such columns.
          </p><p>
            Using <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a> might give you
            some unexpected problems because all calculations in MySQL
            are done with double precision. See
            <a class="xref" href="error-handling.html#no-matching-rows" title="B.4.4.7解决无匹配行的问题">Section&nbsp;B.4.4.7, “Solving Problems with No Matching Rows”</a>.
          </p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694534176"></a>

            <a class="indexterm" name="idm140091694533104"></a>

            <a class="indexterm" name="idm140091694532032"></a>

            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT(<em class="replaceable"><code>p</code></em>)
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p>
            A floating-point number. <em class="replaceable"><code>p</code></em>
            represents the precision in bits, but MySQL uses this value
            only to determine whether to use
            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a> or
            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a> for the resulting data
            type. If <em class="replaceable"><code>p</code></em> is from 0 to 24, the
            data type becomes <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a> with
            no <em class="replaceable"><code>M</code></em> or
            <em class="replaceable"><code>D</code></em> values. If
            <em class="replaceable"><code>p</code></em> is from 25 to 53, the data type
            becomes <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a> with no
            <em class="replaceable"><code>M</code></em> or <em class="replaceable"><code>D</code></em>
            values. The range of the resulting column is the same as for
            the single-precision <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a> or
            double-precision <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a> data
            types described earlier in this section.
          </p><p>
            <code class="literal">UNSIGNED</code>, if specified, disallows
            negative values. As of MySQL 8.0.17, the
            <code class="literal">UNSIGNED</code> attribute is deprecated for
            columns of type <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a> (and
            any synonyms) and will be removed in a future MySQL version.
            Consider using a simple <code class="literal">CHECK</code> constraint
            instead for such columns.
          </p><p>
            <a class="indexterm" name="idm140091694513312"></a>

            <a class="indexterm" name="idm140091694512240"></a>

            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT(<em class="replaceable"><code>p</code></em>)</code></a>
            syntax is provided for ODBC compatibility.
          </p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694506256"></a>

            <a class="indexterm" name="idm140091694505184"></a>

            <a class="indexterm" name="idm140091694504112"></a>

            <a class="indexterm" name="idm140091694502624"></a>

            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE[(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p>
            A normal-size (double-precision) floating-point number.
            Permissible values are
            <code class="literal">-1.7976931348623157E+308</code> to
            <code class="literal">-2.2250738585072014E-308</code>,
            <code class="literal">0</code>, and
            <code class="literal">2.2250738585072014E-308</code> to
            <code class="literal">1.7976931348623157E+308</code>. These are the
            theoretical limits, based on the IEEE standard. The actual
            range might be slightly smaller depending on your hardware
            or operating system.
          </p><p>
            <em class="replaceable"><code>M</code></em> is the total number of digits
            and <em class="replaceable"><code>D</code></em> is the number of digits
            following the decimal point. If <em class="replaceable"><code>M</code></em>
            and <em class="replaceable"><code>D</code></em> are omitted, values are
            stored to the limits permitted by the hardware. A
            double-precision floating-point number is accurate to
            approximately 15 decimal places.
          </p><p>
            <code class="literal">DOUBLE(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code>
            is a nonstandard MySQL extension. As of MySQL 8.0.17, this
            syntax is deprecated and support for it will be removed in a
            future MySQL version.
          </p><p>
            <code class="literal">UNSIGNED</code>, if specified, disallows
            negative values. As of MySQL 8.0.17, the
            <code class="literal">UNSIGNED</code> attribute is deprecated for
            columns of type <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a> (and
            any synonyms) and will be removed in a future MySQL version.
            Consider using a simple <code class="literal">CHECK</code> constraint
            instead for such columns.
          </p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694483776"></a>

            <a class="indexterm" name="idm140091694482688"></a>

            <a class="indexterm" name="idm140091694481616"></a>

            <a class="indexterm" name="idm140091694480128"></a>

            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE
            PRECISION[(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>,
            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">REAL[(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)]
            [UNSIGNED] [ZEROFILL]</code></a>
          </p><p>
            These types are synonyms for
            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a>. Exception: If the
            <a class="link" href="server-administration.html#sqlmode_real_as_float"><code class="literal">REAL_AS_FLOAT</code></a> SQL mode is
            enabled, <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">REAL</code></a> is a synonym
            for <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a> rather than
            <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a>.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="date-and-time-type-overview"></a>11.1.2&nbsp;Date and Time Type Overview</h3>

</div>

</div>

</div>
<p>
        A summary of the temporal data types follows. For additional
        information about properties and storage requirements of the
        temporal types, see <a class="xref" href="data-types.html#date-and-time-types" title="11。3日期和时间类型">Section&nbsp;11.3, “Date and Time Types”</a>, and
        <a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求">Section&nbsp;11.8, “Data Type Storage Requirements”</a>. For descriptions of
        functions that operate on temporal values, see
        <a class="xref" href="functions.html#date-and-time-functions" title="12。7日期和时间函数">Section&nbsp;12.7, “Date and Time Functions”</a>.
      </p><p>
        For the <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATE</code></a> and
        <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a> range descriptions,
        <span class="quote">“<span class="quote">supported</span>”</span> means that although earlier values
        might work, there is no guarantee.
      </p><a class="indexterm" name="idm140091694459648"></a><a class="indexterm" name="idm140091694458560"></a><p>
        MySQL permits fractional seconds for
        <a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a>,
        <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a>, and
        <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a> values, with up to
        microseconds (6 digits) precision. To define a column that
        includes a fractional seconds part, use the syntax
        <code class="literal"><em class="replaceable"><code>type_name</code></em>(<em class="replaceable"><code>fsp</code></em>)</code>,
        where <em class="replaceable"><code>type_name</code></em> is
        <a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a>,
        <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a>, or
        <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a>, and
        <em class="replaceable"><code>fsp</code></em> is the fractional seconds
        precision. For example:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (t TIME(3), dt DATETIME(6));
</pre><p>
        The <em class="replaceable"><code>fsp</code></em> value, if given, must be in
        the range 0 to 6. A value of 0 signifies that there is no
        fractional part. If omitted, the default precision is 0. (This
        differs from the standard SQL default of 6, for compatibility
        with previous MySQL versions.)
      </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表中的</font><font style="vertical-align: inherit;">
        任何</font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
         </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列都可以具有自动初始化和更新属性。
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <a class="indexterm" name="idm140091694438992"></a>

            <a class="indexterm" name="idm140091694437920"></a>

            <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATE</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一个约会。</font><font style="vertical-align: inherit;">支持的范围是
             </font></font><code class="literal">'1000-01-01'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到
             </font></font><code class="literal">'9999-12-31'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">MySQL </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以</font></font><code class="literal">'YYYY-MM-DD'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">格式</font><font style="vertical-align: inherit;">显示
             </font><font style="vertical-align: inherit;">值
             </font><font style="vertical-align: inherit;">，但允许</font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            使用字符串或数字</font><font style="vertical-align: inherit;">将值分配给</font><font style="vertical-align: inherit;">列。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694427408"></a>

            <a class="indexterm" name="idm140091694426336"></a>

            <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME[(<em class="replaceable"><code>fsp</code></em>)]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            日期和时间组合。</font><font style="vertical-align: inherit;">支持的范围是
             </font></font><code class="literal">'1000-01-01 00:00:00.000000'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到
             </font></font><code class="literal">'9999-12-31 23:59:59.999999'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">MySQL </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以</font><font style="vertical-align: inherit;">
            格式</font><font style="vertical-align: inherit;">显示</font><font style="vertical-align: inherit;">值
             </font><font style="vertical-align: inherit;">，但允许</font><font style="vertical-align: inherit;">使用字符串或数字</font><font style="vertical-align: inherit;">将值分配给
             </font><font style="vertical-align: inherit;">列。
          </font></font><code class="literal">'<em class="replaceable"><code>YYYY-MM-DD
            hh:mm:ss</code></em>[.<em class="replaceable"><code>fraction</code></em>]'</code><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"></font><em class="replaceable"><code>fsp</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">可以给出0到6范围内</font><font style="vertical-align: inherit;">
            的可选</font><font style="vertical-align: inherit;">值以指定小数秒精度。</font><font style="vertical-align: inherit;">值为0表示没有小数部分。</font><font style="vertical-align: inherit;">如果省略，则默认精度为0。
          </font></font></p><p><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">可以使用</font></font><code class="literal">DEFAULT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
             </font></font><code class="literal">ON UPDATE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列定义子句</font><font style="vertical-align: inherit;">指定</font><font style="vertical-align: inherit;">
            自动初始化和更新到</font><font style="vertical-align: inherit;">列</font><font style="vertical-align: inherit;">的当前日期和时间</font><font style="vertical-align: inherit;">，如</font></font><a class="xref" href="data-types.html#timestamp-initialization" title="11.3.4 TIMESTAMP和DATETIME的自动初始化和更新"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.3.4节“TIMESTAMP和DATETIME的自动初始化和更新”中所述</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694408864"></a>

            <a class="indexterm" name="idm140091694407792"></a>

            <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP[(<em class="replaceable"><code>fsp</code></em>)]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            时间戳。</font><font style="vertical-align: inherit;">范围是</font></font><code class="literal">'1970-01-01
            00:00:01.000000'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">UTC到</font></font><code class="literal">'2038-01-19
            03:14:07.999999'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">UTC。
            </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值存储为自纪元（</font></font><code class="literal">'1970-01-01 00:00:00'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">UTC）</font><font style="vertical-align: inherit;">以来的秒数</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">甲
             </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">不能代表值</font></font><code class="literal">'1970-01-01 00:00:00'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，因为这是等同于从所述历元和值00秒被保留用于表示</font></font><code class="literal">'0000-00-00
            00:00:00'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，该</font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">“ </font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">零</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> ”</font></font></span>
            <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值。
          </font></font></p><p><font style="vertical-align: inherit;"></font><em class="replaceable"><code>fsp</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">可以给出0到6范围内</font><font style="vertical-align: inherit;">
            的可选</font><font style="vertical-align: inherit;">值以指定小数秒精度。</font><font style="vertical-align: inherit;">值为0表示没有小数部分。</font><font style="vertical-align: inherit;">如果省略，则默认精度为0。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            服务器处理</font></font><code class="literal">TIMESTAMP</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            定义</font><font style="vertical-align: inherit;">的方式</font><font style="vertical-align: inherit;">取决于</font></font><a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            系统变量</font><font style="vertical-align: inherit;">的值
             </font><font style="vertical-align: inherit;">（请参见
             </font></font><a class="xref" href="server-administration.html#server-system-variables" title="5.1.8服务器系统变量"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第5.1.8节“服务器系统变量”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            如果
             </font></font><a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            启用，则不会自动将</font></font><code class="literal">DEFAULT CURRENT_TIMESTAMP</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或</font></font><code class="literal">ON
            UPDATE CURRENT_TIMESTAMP</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性</font><font style="vertical-align: inherit;">分配
             </font><font style="vertical-align: inherit;">给任何
             </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列。</font><font style="vertical-align: inherit;">它们必须明确包含在列定义中。</font><font style="vertical-align: inherit;">此外，任何
             </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">未明确声明为</font></font><code class="literal">NOT NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">许可
             </font></font><code class="literal">NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值的。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            如果
             </font></font><a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            禁用，则服务器处理</font></font><code class="literal">TIMESTAMP</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            如下：
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            除非另有说明，否则</font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表中</font><font style="vertical-align: inherit;">的第一
             </font><font style="vertical-align: inherit;">列定义为如果未明确赋值，则自动设置为最近修改的日期和时间。</font><font style="vertical-align: inherit;">这</font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            对于记录</font></font><a class="link" href="sql-syntax.html#insert" title="13.2.6 INSERT语法"><code class="literal">INSERT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
             </font></font><a class="link" href="sql-syntax.html#update" title="13.2.12 UPDATE语法"><code class="literal">UPDATE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">操作</font><font style="vertical-align: inherit;">的时间戳非常有用
             </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">您还可以</font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">通过为其指定</font></font><code class="literal">NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值</font><font style="vertical-align: inherit;">来</font><font style="vertical-align: inherit;">将任何</font><font style="vertical-align: inherit;">列设置为当前日期和时间
             </font><font style="vertical-align: inherit;">，除非已使用</font></font><code class="literal">NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">允许</font></font><code class="literal">NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值</font><font style="vertical-align: inherit;">的</font><font style="vertical-align: inherit;">属性
             </font><font style="vertical-align: inherit;">定义该列</font><font style="vertical-align: inherit;">。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            可以使用</font></font><code class="literal">DEFAULT
            CURRENT_TIMESTAMP</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">ON UPDATE
            CURRENT_TIMESTAMP</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列定义子句</font><font style="vertical-align: inherit;">指定自动初始化和更新到当前日期和时间</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">默认情况下，第一</font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            列具有这些属性，如前所述。</font><font style="vertical-align: inherit;">但是，</font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">可以将表中的</font><font style="vertical-align: inherit;">任何</font><font style="vertical-align: inherit;">列定义为具有这些属性。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694365136"></a>

            <a class="indexterm" name="idm140091694364064"></a>

            <a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME[(<em class="replaceable"><code>fsp</code></em>)]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一次。</font><font style="vertical-align: inherit;">范围是</font></font><code class="literal">'-838:59:59.000000'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            到</font></font><code class="literal">'838:59:59.000000'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">MySQL </font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以</font><font style="vertical-align: inherit;">
            格式</font><font style="vertical-align: inherit;">显示
             </font><font style="vertical-align: inherit;">值
             </font><font style="vertical-align: inherit;">，但允许</font><font style="vertical-align: inherit;">使用字符串或数字</font><font style="vertical-align: inherit;">将值分配给
             </font><font style="vertical-align: inherit;">列。
          </font></font><code class="literal">'<em class="replaceable"><code>hh:mm:ss</code></em>[.<em class="replaceable"><code>fraction</code></em>]'</code><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"></font><em class="replaceable"><code>fsp</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">可以给出0到6范围内</font><font style="vertical-align: inherit;">
            的可选</font><font style="vertical-align: inherit;">值以指定小数秒精度。</font><font style="vertical-align: inherit;">值为0表示没有小数部分。</font><font style="vertical-align: inherit;">如果省略，则默认精度为0。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694351344"></a>

            <a class="indexterm" name="idm140091694350272"></a>

            <a class="link" href="data-types.html#year" title="11.3.3年份类型"><code class="literal">YEAR[(4)]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一年四位数格式。</font><font style="vertical-align: inherit;">MySQL </font></font><a class="link" href="data-types.html#year" title="11.3.3年份类型"><code class="literal">YEAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以</font></font><code class="literal">YYYY</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">格式</font><font style="vertical-align: inherit;">显示
             </font><font style="vertical-align: inherit;">值
             </font><font style="vertical-align: inherit;">，但允许</font></font><a class="link" href="data-types.html#year" title="11.3.3年份类型"><code class="literal">YEAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">使用字符串或数字</font><font style="vertical-align: inherit;">将值分配给</font><font style="vertical-align: inherit;">列。</font><font style="vertical-align: inherit;">值显示为
             </font></font><code class="literal">1901</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">到</font></font><code class="literal">2155</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
             </font></font><code class="literal">0000</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            有关</font></font><a class="link" href="data-types.html#year" title="11.3.3年份类型"><code class="literal">YEAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">显示格式和输入值解释的</font><font style="vertical-align: inherit;">更多信息
             </font><font style="vertical-align: inherit;">，请参见</font></font><a class="xref" href="data-types.html#year" title="11.3.3年份类型"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.3.3节“YEAR类型”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
注意
</font></font></div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
              MySQL 8.0不支持</font></font><a class="link" href="data-types.html#year" title="11.3.3年份类型"><code class="literal">YEAR(2)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">旧版MySQL中允许</font><font style="vertical-align: inherit;">的
               </font><font style="vertical-align: inherit;">数据类型。</font><font style="vertical-align: inherit;">有关转换成指令</font></font><a class="link" href="data-types.html#year" title="11.3.3年份类型"><code class="literal">YEAR(4)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，请参见
               </font></font><a class="ulink" href="https://dev.mysql.com/doc/refman/5.7/en/migrating-to-year4.html" target="_top"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">YEAR（2）的限制和迁移到YEAR（4）</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">中
               </font></font><a class="ulink" href="https://dev.mysql.com/doc/refman/5.7/en/" target="_top"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">的MySQL 5.7参考手册</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
</div>
</li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在</font></font><a class="link" href="functions.html#function_sum"><code class="literal">SUM()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="functions.html#function_avg"><code class="literal">AVG()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">聚合函数不具有时间价值的工作。</font><font style="vertical-align: inherit;">（它们将值转换为数字，在第一个非数字字符后丢失所有内容。）要解决此问题，请转换为数字单位，执行聚合操作，然后转换回时间值。</font><font style="vertical-align: inherit;">例子：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">SELECT SEC_TO_TIME（SUM（TIME_TO_SEC（</font></font><em class="replaceable"><code>time_col</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）））FROM </font></font><em class="replaceable"><code>tbl_name</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
SELECT FROM_DAYS（SUM（TO_DAYS（</font></font><em class="replaceable"><code>date_col</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）））FROM </font></font><em class="replaceable"><code>tbl_name</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">;
</font></font></pre>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="string-type-overview"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.1.3字符串类型概述</font></font></h3>

</div>

</div>

</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        下面是字符串数据类型的摘要。</font><font style="vertical-align: inherit;">有关字符串类型的属性和存储要求的其他信息，请参见</font></font><a class="xref" href="data-types.html#string-types" title="11.4字符串类型"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.4节“字符串类型”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.8节“数据类型存储要求”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在某些情况下，MySQL可能会将字符串列更改为与</font></font><a class="link" href="sql-syntax.html#create-table" title="13.1.20 CREATE TABLE语法"><code class="literal">CREATE
        TABLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">or </font></font><a class="link" href="sql-syntax.html#alter-table" title="13.1.9 ALTER TABLE语法"><code class="literal">ALTER TABLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        语句</font><font style="vertical-align: inherit;">中给出的类型不同的类型</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">请参见</font></font><a class="xref" href="sql-syntax.html#silent-column-changes" title="13.1.20.8无声列规范更改"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第13.1.20.8节“无声列规范更改”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL以字符为单位解释字符列定义中的长度规范。</font><font style="vertical-align: inherit;">这适用于
         </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        用于字符串的数据类型列定义
         </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">的
         </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型，
         </font></font><a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，和任何同义词）可以指定列字符集和归类：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">CHARACTER SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">指定字符集。</font><font style="vertical-align: inherit;">如果需要，可以使用</font></font><code class="literal">COLLATE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性以及任何其他属性</font><font style="vertical-align: inherit;">指定字符集的排序规则</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">例如：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">创建表t</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
（</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    c1 VARCHAR（20）CHARACTER SET utf8，</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    c2 TEXT CHARACTER SET latin1 COLLATE latin1_general_cs</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
）;</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            此表定义创建一个名为a的列</font></font><code class="literal">c1</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，</font><font style="vertical-align: inherit;">该列
             </font><font style="vertical-align: inherit;">具有该字符集
             </font></font><code class="literal">utf8</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">的默认排序规则的字符集，以及一个名称</font></font><code class="literal">c2</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">具有字符集</font></font><code class="literal">latin1</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和区分大小写的排序规则的列。
          </font></font></p><p><font style="vertical-align: inherit;"><a class="xref" href="charset.html#charset-column" title="10.3.5列字符集和校对"><font style="vertical-align: inherit;">第10.3.5节“列字符集和排序规则”</font></a><font style="vertical-align: inherit;">中介绍了在缺少属性</font></font><code class="literal">CHARACTER SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
             </font></font><code class="literal">COLLATE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性</font><font style="vertical-align: inherit;">
            时分配字符集和排序</font></font><a class="xref" href="charset.html#charset-column" title="10.3.5列字符集和校对"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">规则的规则</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p><p>
            <code class="literal">CHARSET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是...的同义词
             </font></font><code class="literal">CHARACTER SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            指定</font></font><code class="literal">CHARACTER SET binary</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            字符串数据类型</font><font style="vertical-align: inherit;">的</font><font style="vertical-align: inherit;">属性会导致将列创建为相应的二进制字符串数据类型：
             </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">变为
             </font></font><a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">BINARY</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
             </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">变为
             </font></font><a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">VARBINARY</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
             </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">变为
             </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">对于
             </font></font><a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
             </font></font><a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型，不会发生这种情况; </font><font style="vertical-align: inherit;">它们是按声明创建的。</font><font style="vertical-align: inherit;">假设您使用此定义指定表：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">创建表t</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
（</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c1 VARCHAR（10）CHARACTER SET二进制，</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c2 TEXT CHARACTER SET二进制，</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c3 ENUM（'a'，'b'，'c'）CHARACTER SET二进制</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
）;</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            结果表具有以下定义：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">创建表t</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
（</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c1 VARBINARY（10），</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c2 BLOB，</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c3 ENUM（'a'，'b'，'c'）CHARACTER SET二进制</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
）;</font></font><font></font>
</pre></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            该</font></font><code class="literal">BINARY</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性是一个非标准的MySQL扩展，它是指定</font></font><code class="literal">_bin</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列字符集（如果未指定列字符集，则为表默认字符集）</font><font style="vertical-align: inherit;">的二进制（</font><font style="vertical-align: inherit;">）排序规则的</font><font style="vertical-align: inherit;">简写</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">在这种情况下，比较和排序基于数字字符代码值。</font><font style="vertical-align: inherit;">假设您使用此定义指定表：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">创建表t</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
（</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c1 VARCHAR（10）CHARACTER SET latin1 BINARY，</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c2 TEXT BINARY</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
）CHARACTER SET utf8mb4;</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            结果表具有以下定义：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">CREATE TABLE t（</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c1 VARCHAR（10）CHARACTER SET latin1 COLLATE latin1_bin，</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  c2 TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
）CHARACTER SET utf8mb4;</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            在MySQL 8.0中，该</font></font><code class="literal">BINARY</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性的</font><font style="vertical-align: inherit;">非标准使用
             </font><font style="vertical-align: inherit;">是不明确的，因为
             </font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符集具有多个
             </font></font><code class="literal">_bin</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">排序规则。</font><font style="vertical-align: inherit;">从MySQL 8.0.17开始，</font></font><code class="literal">BINARY</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">不推荐使用</font><font style="vertical-align: inherit;">该
             </font><font style="vertical-align: inherit;">属性，并且在将来的MySQL版本中将删除对它的支持。</font><font style="vertical-align: inherit;">应调整应用程序以使用显式
             </font></font><code class="literal">_bin</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">排序规则。
          </font></font></p><p><font style="vertical-align: inherit;"></font><code class="literal">BINARY</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">指定数据类型或字符集</font><font style="vertical-align: inherit;">
            的用法</font><font style="vertical-align: inherit;">保持不变。
          </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            该</font></font><code class="literal">ASCII</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性是简写
             </font></font><code class="literal">CHARACTER SET latin1</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            该</font></font><code class="literal">UNICODE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性是简写
             </font></font><code class="literal">CHARACTER SET ucs2</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        字符列比较和排序基于分配给列的排序规则。</font><font style="vertical-align: inherit;">对于
         </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，和
         </font></font><a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型，可以使用二进制（声明一个柱</font></font><code class="literal">_bin</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）归类或所述
         </font></font><code class="literal">BINARY</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性，以使比较和排序，以使用底层字符代码值，而不是一个词汇顺序。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        有关在MySQL中使用字符集的其他信息，请参阅</font></font><a class="xref" href="charset.html" title="第10章字符集，排序规则，Unicode"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第10章，</font></font><i><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符集，排序规则，Unicode</font></font></i></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <a class="indexterm" name="idm140091694255488"></a>

            <a class="indexterm" name="idm140091694254416"></a>

            <a class="indexterm" name="idm140091694253344"></a>

            <a class="indexterm" name="idm140091694252272"></a>

            <a class="indexterm" name="idm140091694251200"></a>

            <a class="indexterm" name="idm140091694249712"></a>

            <a class="indexterm" name="idm140091694248224"></a>

            <a class="indexterm" name="idm140091694246736"></a>

            <code class="literal">[NATIONAL] CHAR[(<em class="replaceable"><code>M</code></em>)]
            [CHARACTER SET <em class="replaceable"><code>charset_name</code></em>]
            [COLLATE
            <em class="replaceable"><code>collation_name</code></em>]</code>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一个固定长度的字符串，在存储时始终用空格填充指定的长度。
            </font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表示以字符为单位的列长度。</font><font style="vertical-align: inherit;">范围</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">为0到255.如果</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">省略，则长度为1。
</font></font></p><a class="indexterm" name="idm140091694241232"></a>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
注意
</font></font></div>
<p><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">除非</font></font><a class="link" href="server-administration.html#sqlmode_pad_char_to_full_length"><code class="literal">PAD_CHAR_TO_FULL_LENGTH</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
              启用S​​QL模式，</font><font style="vertical-align: inherit;">否则在检索值
               </font><font style="vertical-align: inherit;">
              时将删除尾随空格
               </font><font style="vertical-align: inherit;">。
</font></font></p>
</div>
<p>
            <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是简写
             </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHARACTER</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
            </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">NATIONAL CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（或其等效的简短形式</font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">NCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）是定义</font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列应使用某些预定义字符集</font><font style="vertical-align: inherit;">的标准SQL方法
             </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">MySQL使用</font></font><code class="literal">utf8</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            此预定义字符集。
            </font></font><a class="xref" href="charset.html#charset-national" title="10.3.7国家字符集"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第10.3.7节“国家字符集”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            的</font></font><a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">CHAR BYTE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型是用于一个别名</font></font><a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">BINARY</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">的数据类型。</font><font style="vertical-align: inherit;">这是兼容性功能。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            MySQL允许您创建类型的列
             </font></font><code class="literal">CHAR(0)</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">当您必须符合依赖于列的存在但实际上不使用其值的旧应用程序时，这非常有用。</font></font><code class="literal">CHAR(0)</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">当你需要一个只能占用两个值的列时，这也是非常好的：定义为</font></font><code class="literal">CHAR(0) NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            只占用</font><font style="vertical-align: inherit;">一位的列，</font><font style="vertical-align: inherit;">只能取值
             </font></font><code class="literal">NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">''</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（空字符串）。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694214992"></a>

            <a class="indexterm" name="idm140091694213920"></a>

            <a class="indexterm" name="idm140091694212832"></a>

            <a class="indexterm" name="idm140091694211760"></a>

            <a class="indexterm" name="idm140091694210688"></a>

            <a class="indexterm" name="idm140091694209600"></a>

            <a class="indexterm" name="idm140091694208528"></a>

            <a class="indexterm" name="idm140091694207040"></a>

            <a class="indexterm" name="idm140091694205552"></a>

            <a class="indexterm" name="idm140091694204064"></a>

            <a class="indexterm" name="idm140091694202576"></a>

            <a class="indexterm" name="idm140091694201088"></a>

            <code class="literal">[NATIONAL] VARCHAR(<em class="replaceable"><code>M</code></em>)
            [CHARACTER SET <em class="replaceable"><code>charset_name</code></em>]
            [COLLATE
            <em class="replaceable"><code>collation_name</code></em>]</code>
          </p><a class="indexterm" name="idm140091694197472"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            可变长度的字符串。</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            表示字符的最大列长度。</font><font style="vertical-align: inherit;">范围</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是0到65,535。</font><font style="vertical-align: inherit;">a的有效最大长度
             </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">取决于最大行大小（65,535字节，在所有列之间共享）和使用的字符集。</font><font style="vertical-align: inherit;">例如，
             </font></font><code class="literal">utf8</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符每个字符最多可能需要三个字节，因此</font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">使用该</font></font><code class="literal">utf8</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符集</font><font style="vertical-align: inherit;">的
             </font><font style="vertical-align: inherit;">列
             </font><font style="vertical-align: inherit;">可以声明为最多21,844个字符。</font><font style="vertical-align: inherit;">请参见
             </font></font><a class="xref" href="restrictions.html#column-count-limit" title="C.10.4表列数和行大小的限制"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第C.10.4节“表列数和行大小的限制”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            MySQL将</font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值</font><font style="vertical-align: inherit;">存储</font><font style="vertical-align: inherit;">为1字节或2字节长度前缀加数据。</font><font style="vertical-align: inherit;">长度前缀表示值中的字节数。</font><font style="vertical-align: inherit;">甲
             </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列使用一个长度字节的值，如果不需要超过255字节，2个字节长度值是否可能需要多于255个字节。
</font></font></p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
注意
</font></font></div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
              MySQL的遵从标准SQL规范，并
               </font></font><span class="emphasis"><em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">没有</font></font></em></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">从删除尾随空格
               </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">的值。
</font></font></p>
</div>
<p>
            <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是简写
             </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHARACTER VARYING</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
            </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">NATIONAL VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是标准SQL方法，用于定义
             </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列应使用某些预定义字符集。</font><font style="vertical-align: inherit;">MySQL使用
             </font></font><code class="literal">utf8</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">此预定义字符集。
            </font></font><a class="xref" href="charset.html#charset-national" title="10.3.7国家字符集"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第10.3.7节“国家字符集”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
            </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">NVARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是简写
             </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">NATIONAL VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694171952"></a>

            <a class="indexterm" name="idm140091694170880"></a>

            <a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">BINARY[(<em class="replaceable"><code>M</code></em>)]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            该</font></font><a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">BINARY</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型是类似的</font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型，但保存二进制字节字符串而不非二进制字符串。</font><font style="vertical-align: inherit;">可选长度</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表示以字节为单位的列长度。</font><font style="vertical-align: inherit;">如果省略，则
             </font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">默认为1。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694161264"></a>

            <a class="indexterm" name="idm140091694160192"></a>

            <a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">VARBINARY(<em class="replaceable"><code>M</code></em>)</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            该</font></font><a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">VARBINARY</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型是类似的</font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型，但保存二进制字节字符串而不非二进制字符串。</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表示最大列长度（以字节为单位）。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694151056"></a>

            <a class="indexterm" name="idm140091694149984"></a>

            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TINYBLOB</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            阿</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">为255字节的最大长度塔（2 </font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">8</font></font></sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> - 1）个字节。</font><font style="vertical-align: inherit;">每个</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TINYBLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值使用1字节长度前缀存储，该前缀指示值中的字节数。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694141184"></a>

            <a class="indexterm" name="idm140091694140112"></a>

            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TINYTEXT
            [CHARACTER SET <em class="replaceable"><code>charset_name</code></em>]
            [COLLATE
            <em class="replaceable"><code>collation_name</code></em>]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            阿</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">与255（2的最大长度列</font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">8</font></font></sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">个字符- 1）。</font><font style="vertical-align: inherit;">如果值包含多字节字符，则有效最大长度较小。</font><font style="vertical-align: inherit;">每个
             </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TINYTEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值使用1字节长度前缀存储，该前缀指示值中的字节数。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694130192"></a>

            <a class="indexterm" name="idm140091694129120"></a>

            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB[(<em class="replaceable"><code>M</code></em>)]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            阿</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">用的65,535（2的最大长度柱</font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">16</font></font></sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> - 1）字节。</font><font style="vertical-align: inherit;">每个</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值使用2字节长度前缀存储，该前缀指示值中的字节数。
          </font></font></p><p><font style="vertical-align: inherit;"></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">可以为此类型指定</font><font style="vertical-align: inherit;">
            可选长度</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">如果这样做，MySQL </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">会将</font><font style="vertical-align: inherit;">列创建为</font><font style="vertical-align: inherit;">足够大</font><font style="vertical-align: inherit;">的最小</font><font style="vertical-align: inherit;">类型，以保存</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">long bytes的</font><font style="vertical-align: inherit;">值</font><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694117296"></a>

            <a class="indexterm" name="idm140091694116224"></a>

            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT[(<em class="replaceable"><code>M</code></em>)]
            [CHARACTER SET <em class="replaceable"><code>charset_name</code></em>]
            [COLLATE
            <em class="replaceable"><code>collation_name</code></em>]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列，最大长度为65,535（2 </font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">16</font></font></sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> -  1）个字符。</font><font style="vertical-align: inherit;">如果值包含多字节字符，则有效最大长度较小。</font><font style="vertical-align: inherit;">每个
             </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值使用2字节长度前缀存储，该前缀指示值中的字节数。
          </font></font></p><p><font style="vertical-align: inherit;"></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">可以为此类型指定</font><font style="vertical-align: inherit;">
            可选长度</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">如果这样做，MySQL </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">会将</font><font style="vertical-align: inherit;">列创建为</font><font style="vertical-align: inherit;">足够大</font><font style="vertical-align: inherit;">的最小</font><font style="vertical-align: inherit;">类型，以保存值</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            字符长。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694103232"></a>

            <a class="indexterm" name="idm140091694102160"></a>

            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMBLOB</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列，最大长度为16,777,215（2 </font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">24</font></font></sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> -  1）个字节。</font><font style="vertical-align: inherit;">每个</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMBLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            值使用3字节长度前缀存储，该前缀指示值中的字节数。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694093296"></a>

            <a class="indexterm" name="idm140091694092224"></a>

            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMTEXT
            [CHARACTER SET <em class="replaceable"><code>charset_name</code></em>]
            [COLLATE
            <em class="replaceable"><code>collation_name</code></em>]</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            一</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列，最大长度为16,777,215（2 </font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">24</font></font></sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> -  1）个字符。</font><font style="vertical-align: inherit;">如果值包含多字节字符，则有效最大长度较小。</font><font style="vertical-align: inherit;">每个
             </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMTEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值使用3字节长度前缀存储，该前缀指示值中的字节数。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694081584"></a>

            <a class="indexterm" name="idm140091694080512"></a>

            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGBLOB</code></a>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            甲</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（2具有4294967295或4GB的最大长度柱</font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">32</font></font></sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> - 1）个字节。</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGBLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列</font><font style="vertical-align: inherit;">的有效最大长度
             </font><font style="vertical-align: inherit;">取决于客户端/服务器协议中配置的最大数据包大小和可用内存。</font><font style="vertical-align: inherit;">每个
             </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGBLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值使用4字节长度前缀存储，该前缀指示值中的字节数。
          </font></font></p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694070240"></a>

            <a class="indexterm" name="idm140091694069168"></a>

            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGTEXT
            [CHARACTER SET <em class="replaceable"><code>charset_name</code></em>]
            [COLLATE
            <em class="replaceable"><code>collation_name</code></em>]</code></a>
          </p><p>
            A <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a> column with a maximum
            length of 4,294,967,295 or 4GB
            (2<sup>32</sup> − 1) characters. The
            effective maximum length is less if the value contains
            multibyte characters. The effective maximum length of
            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGTEXT</code></a>
            columns also depends on the configured maximum packet size
            in the client/server protocol and available memory. Each
            <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGTEXT</code></a>
            value is stored using a 4-byte length prefix that indicates
            the number of bytes in the value.
          </p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694057792"></a>

            <a class="indexterm" name="idm140091694056720"></a>

            <a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM('<em class="replaceable"><code>value1</code></em>','<em class="replaceable"><code>value2</code></em>',...)
            [CHARACTER SET <em class="replaceable"><code>charset_name</code></em>]
            [COLLATE
            <em class="replaceable"><code>collation_name</code></em>]</code></a>
          </p><p>
            An enumeration. A string object that can have only one
            value, chosen from the list of values
            <code class="literal">'<em class="replaceable"><code>value1</code></em>'</code>,
            <code class="literal">'<em class="replaceable"><code>value2</code></em>'</code>,
            <code class="literal">...</code>, <code class="literal">NULL</code> or the
            special <code class="literal">''</code> error value.
            <a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM</code></a> values are represented
            internally as integers.
          </p><p>
            An <a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM</code></a> column can have a
            maximum of 65,535 distinct elements.
          </p><p>
            The maximum supported length of an individual
            <code class="literal">ENUM</code> element is
            <em class="replaceable"><code>M</code></em> &lt;= 255 and
            (<em class="replaceable"><code>M</code></em> x
            <em class="replaceable"><code>w</code></em>) &lt;= 1020, where
            <code class="literal">M</code> is the element literal length and
            <em class="replaceable"><code>w</code></em> is the number of bytes required
            for the maximum-length character in the character set.
          </p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091694038000"></a>

            <a class="indexterm" name="idm140091694036928"></a>

            <a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET('<em class="replaceable"><code>value1</code></em>','<em class="replaceable"><code>value2</code></em>',...)
            [CHARACTER SET <em class="replaceable"><code>charset_name</code></em>]
            [COLLATE
            <em class="replaceable"><code>collation_name</code></em>]</code></a>
          </p><p>
            A set. A string object that can have zero or more values,
            each of which must be chosen from the list of values
            <code class="literal">'<em class="replaceable"><code>value1</code></em>'</code>,
            <code class="literal">'<em class="replaceable"><code>value2</code></em>'</code>,
            <code class="literal">...</code> <a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET</code></a>
            values are represented internally as integers.
          </p><p>
            A <a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET</code></a> column can have a
            maximum of 64 distinct members.
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            单个</font></font><code class="literal">SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">元素</font><font style="vertical-align: inherit;">的最大支持长度
             </font><font style="vertical-align: inherit;">为
             </font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">&lt;= 255和（</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">x
             </font></font><em class="replaceable"><code>w</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）&lt;= 1020，其中
             </font></font><code class="literal">M</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是元素文字长度，
             </font></font><em class="replaceable"><code>w</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是字符集中最大长度字符所需的字节数。
</font></font></p></li></ul>
</div>

</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="numeric-types"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2数字类型</font></font></h2>

</div>

</div>

</div>
<div class="toc">
<dl class="toc"><dt><span class="section"><a href="data-types.html#integer-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#fixed-point-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.2定点类型（精确值） -  DECIMAL，NUMERIC</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#floating-point-types"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.3浮点类型（近似值） -  FLOAT，DOUBLE</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#bit-type"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.4比特值类型 -  BIT</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#numeric-type-attributes"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.5数字类型属性</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#out-of-range-and-overflow"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.6超出范围和溢出处理</font></font></a></span></dt></dl>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      MySQL支持所有标准SQL数值数据类型。</font><font style="vertical-align: inherit;">这些类型包括确切的数值数据类型（</font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INTEGER</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
       </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">SMALLINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
       </font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，和
       </font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">NUMERIC</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">），以及该近似数值数据类型（</font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
       </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">REAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，和
       </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE PRECISION</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）。</font><font style="vertical-align: inherit;">关键字
       </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是
       </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INTEGER</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">关键字</font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DEC</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
       </font><font style="vertical-align: inherit;">关键字
       </font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">FIXED</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是同义词
       </font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">MySQL将其
       </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">视为</font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE PRECISION</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（非标准扩展）</font><font style="vertical-align: inherit;">的同义词
       </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">
      除非</font><font style="vertical-align: inherit;">启用S​​QL模式</font><font style="vertical-align: inherit;">，否则</font><font style="vertical-align: inherit;">MySQL还将其视为</font><font style="vertical-align: inherit;">（非标准变体）
       </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">REAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      的同义词</font><font style="vertical-align: inherit;">。
    </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE PRECISION</code></a><font style="vertical-align: inherit;"></font><a class="link" href="server-administration.html#sqlmode_real_as_float"><code class="literal">REAL_AS_FLOAT</code></a><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      该</font></font><a class="link" href="data-types.html#bit-type" title="11.2.4比特值类型 -  BIT"><code class="literal">BIT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型存储位值，并且被支撑为</font></font><a class="link" href="storage-engines.html#myisam-storage-engine" title="16.2 MyISAM存储引擎"><code class="literal">MyISAM</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
       </font></font><a class="link" href="storage-engines.html#memory-storage-engine" title="16.3 MEMORY存储引擎"><code class="literal">MEMORY</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
       </font></font><a class="link" href="innodb-storage-engine.html" title="第15章InnoDB存储引擎"><code class="literal">InnoDB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，和
       </font></font><a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      有关MySQL如何处理在表达式评估期间将超出范围值分配给列和溢出的信息，请参见
       </font></font><a class="xref" href="data-types.html#out-of-range-and-overflow" title="11.2.6超出范围和溢出处理"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.2.6节“超出范围和溢出处理”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      有关数字类型存储要求的信息，请参见
       </font></font><a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.8节“数据类型存储要求”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      用于计算数字操作数的结果的数据类型取决于操作数的类型和对它们执行的操作。</font><font style="vertical-align: inherit;">有关更多信息，请参见
       </font></font><a class="xref" href="functions.html#arithmetic-functions" title="12.6.1算术运算符"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第12.6.1节“算术运算符”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
<div class="section">

<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="integer-types"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT</font></font></h3>
</div>
</div>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL支持SQL标准整数类型
         </font></font><code class="literal">INTEGER</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（或</font></font><code class="literal">INT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）和
         </font></font><code class="literal">SMALLINT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">作为一个可扩展标准，MySQL也支持整数类型
         </font></font><code class="literal">TINYINT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，</font></font><code class="literal">MEDIUMINT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><code class="literal">BIGINT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">下表显示了每种整数类型所需的存储和范围。
</font></font></p>
<div class="table">
<a name="integer-type-storage-and-range"></a><p class="title"><b><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表11.1 MySQL支持的整数类型所需的存储和范围</font></font></b></p>
<div class="table-contents">
<table summary="Required storage and range for integer types supported by MySQL. Information includes the integer type, the storage size in bytes, the minimum signed and unsigned values, and the maximum signed and unsigned values."><colgroup><col width="16%"><col width="16%"><col width="16%"><col width="16%"><col width="16%"><col width="16%"></colgroup><thead><tr>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型</font></font></th>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">存储（字节）</font></font></th>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">最小值签名</font></font></th>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">最小值无符号</font></font></th>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">最大值签名</font></font></th>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">最大值无符号</font></font></th>
          </tr></thead><tbody><tr>
            <td scope="row"><code class="literal">TINYINT</code></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">1</font></font></td>
            <td><code class="literal">-128</code></td>
            <td><code class="literal">0</code></td>
            <td><code class="literal">127</code></td>
            <td><code class="literal">255</code></td>
          </tr><tr>
            <td scope="row"><code class="literal">SMALLINT</code></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">2</font></font></td>
            <td><code class="literal">-32768</code></td>
            <td><code class="literal">0</code></td>
            <td><code class="literal">32767</code></td>
            <td><code class="literal">65535</code></td>
          </tr><tr>
            <td scope="row"><code class="literal">MEDIUMINT</code></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">3</font></font></td>
            <td><code class="literal">-8388608</code></td>
            <td><code class="literal">0</code></td>
            <td><code class="literal">8388607</code></td>
            <td><code class="literal">16777215</code></td>
          </tr><tr>
            <td scope="row"><code class="literal">INT</code></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">4</font></font></td>
            <td><code class="literal">-2147483648</code></td>
            <td><code class="literal">0</code></td>
            <td><code class="literal">2147483647</code></td>
            <td><code class="literal">4294967295</code></td>
          </tr><tr>
            <td scope="row"><code class="literal">BIGINT</code></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">8</font></font></td>
            <td><code class="literal">-2<sup>63</sup></code></td>
            <td><code class="literal">0</code></td>
            <td><code class="literal">2<sup>63</sup>-1</code></td>
            <td><code class="literal">2<sup>64</sup>-1</code></td>
</tr></tbody></table>
</div>

</div>
<br class="table-break">
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="fixed-point-types"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.2定点类型（精确值） -  DECIMAL，NUMERIC</font></font></h3>

</div>

</div>

</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        该</font></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">NUMERIC</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        类型的存储精确的数值数据。</font><font style="vertical-align: inherit;">在保持精确精度很重要时使用这些类型，例如使用货币数据。</font><font style="vertical-align: inherit;">在MySQL中，</font></font><code class="literal">NUMERIC</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">实现为</font></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，所以下面的注释</font></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">同样适用于
         </font></font><code class="literal">NUMERIC</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL </font></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以二进制格式</font><font style="vertical-align: inherit;">存储</font><font style="vertical-align: inherit;">值。</font><font style="vertical-align: inherit;">请参见</font></font><a class="xref" href="functions.html#precision-math" title="12.25精度数学"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第12.25节“精确数学”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在</font></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列声明中，可以（通常是）指定精度和小数位数。</font><font style="vertical-align: inherit;">例如：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">工资DECIMAL（5,2）
</font></font></pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在这个例子中，</font></font><code class="literal">5</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是精度，
         </font></font><code class="literal">2</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是规模。</font><font style="vertical-align: inherit;">精度表示为值存储的有效位数，刻度表示小数点后可存储的位数。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        标准SQL要求</font></font><code class="literal">DECIMAL(5,2)</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">能够存储五位数和两位小数的任何值，因此可以存储在</font></font><code class="literal">salary</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        列中的值的范围</font></font><code class="literal">-999.99</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是
         </font></font><code class="literal">999.99</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在标准SQL中，语法
         </font><font style="vertical-align: inherit;">等效于
         </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">类似地，语法</font><font style="vertical-align: inherit;">等同于</font><font style="vertical-align: inherit;">允许实现决定值
         </font><font style="vertical-align: inherit;">的语法</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">MySQL支持这两种变体形式的</font><font style="vertical-align: inherit;">语法。</font><font style="vertical-align: inherit;">默认值为</font><font style="vertical-align: inherit;">10。
      </font></font><code class="literal">DECIMAL(<em class="replaceable"><code>M</code></em>)</code><font style="vertical-align: inherit;"></font><code class="literal">DECIMAL(<em class="replaceable"><code>M</code></em>,0)</code><font style="vertical-align: inherit;"></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"></font><code class="literal">DECIMAL(<em class="replaceable"><code>M</code></em>,0)</code><font style="vertical-align: inherit;"></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如果比例为0，则</font></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值不包含小数点或小数部分。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        最大位数为</font></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">65，但给定</font></font><code class="literal">DECIMAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        列</font><font style="vertical-align: inherit;">的实际范围可以通过给定</font><font style="vertical-align: inherit;">列的精度或比例来约束。</font><font style="vertical-align: inherit;">如果为这样的列分配了小数点后面的位数超过指定比例允许的值，则该值将转换为该比例。</font><font style="vertical-align: inherit;">（精确的行为是特定于操作系统的，但通常效果是截断到允许的位数。）
</font></font></p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="floating-point-types"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.3浮点类型（近似值） -  FLOAT，DOUBLE</font></font></h3>

</div>

</div>

</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        的</font></font><code class="literal">FLOAT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">DOUBLE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型代表近似数字数据值。</font><font style="vertical-align: inherit;">MySQL对于单精度值使用四个字节，对于双精度值使用八个字节。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        因为</font></font><code class="literal">FLOAT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，SQL标准允许</font></font><code class="literal">FLOAT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在括号中</font><font style="vertical-align: inherit;">的关键字</font><font style="vertical-align: inherit;">后面的</font><font style="vertical-align: inherit;">位中选择性地指定精度（但不是指数的范围）
         </font><font style="vertical-align: inherit;">; </font><font style="vertical-align: inherit;">; </font><font style="vertical-align: inherit;">就是，
         </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">MySQL还支持此可选的精度规范，但精度值
         </font><font style="vertical-align: inherit;">
        仅用于确定存储大小。</font><font style="vertical-align: inherit;">精度从0到23会产生一个4字节的单精度</font><font style="vertical-align: inherit;">
        列。</font><font style="vertical-align: inherit;">精度从24到53会产生一个8字节的双精度</font><font style="vertical-align: inherit;">列。
      </font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT(<em class="replaceable"><code>p</code></em>)</code></a><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT(<em class="replaceable"><code>p</code></em>)</code></a><font style="vertical-align: inherit;"></font><code class="literal">FLOAT</code><font style="vertical-align: inherit;"></font><code class="literal">DOUBLE</code><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL允许非标准语法：
         </font><font style="vertical-align: inherit;">
        或
         </font><font style="vertical-align: inherit;">
        或</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">这里，
         </font><font style="vertical-align: inherit;">
        除了值之外，还可以存储多达
         </font><font style="vertical-align: inherit;">数字的数字，其中的
         </font><font style="vertical-align: inherit;">数字可以在小数点后面。</font><font style="vertical-align: inherit;">例如，定义的列
         </font><font style="vertical-align: inherit;">将
         </font><font style="vertical-align: inherit;">在显示时显示。</font><font style="vertical-align: inherit;">MySQL能够执行存储值时，舍入，所以如果插入
         </font><font style="vertical-align: inherit;">到一个
         </font><font style="vertical-align: inherit;">列中，近似的结果是
         </font><font style="vertical-align: inherit;">。
      </font></font><code class="literal">FLOAT(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code><font style="vertical-align: inherit;"></font><code class="literal">REAL(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code><font style="vertical-align: inherit;"></font><code class="literal">DOUBLE
        PRECISION(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code><font style="vertical-align: inherit;"></font><code class="literal">(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code><font style="vertical-align: inherit;"></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"></font><em class="replaceable"><code>D</code></em><font style="vertical-align: inherit;"></font><code class="literal">FLOAT(7,4)</code><font style="vertical-align: inherit;"></font><code class="literal">-999.9999</code><font style="vertical-align: inherit;"></font><code class="literal">999.00009</code><font style="vertical-align: inherit;"></font><code class="literal">FLOAT(7,4)</code><font style="vertical-align: inherit;"></font><code class="literal">999.0001</code><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        从MySQL 8.0.17开始，</font><font style="vertical-align: inherit;">
        不推荐使用</font><font style="vertical-align: inherit;">非标准
         </font><font style="vertical-align: inherit;">
        和
         </font><font style="vertical-align: inherit;">语法，并且在将来的MySQL版本中将删除对它的支持。
      </font></font><code class="literal">FLOAT(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code><font style="vertical-align: inherit;"></font><code class="literal">DOUBLE(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        由于浮点值是近似值而未存储为精确值，因此尝试在比较中将它们视为精确值可能会导致问题。</font><font style="vertical-align: inherit;">它们还受平台或实现依赖性的影响。</font><font style="vertical-align: inherit;">有关更多信息，请参见
         </font></font><a class="xref" href="error-handling.html#problems-with-float" title="B.4.4.8浮点值的问题"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第B.4.4.8节“浮点值的问题”</font></font></a>
      </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        为了获得最大的可移植性，需要存储近似数值数据值的代码应使用</font></font><code class="literal">FLOAT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
         </font></font><code class="literal">DOUBLE PRECISION</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">不</font><font style="vertical-align: inherit;">使用</font><font style="vertical-align: inherit;">精度或数字位数。
</font></font></p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="bit-type"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.4比特值类型 -  BIT</font></font></h3>

</div>

</div>

</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        该</font></font><code class="literal">BIT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型被用于存储比特值。</font><font style="vertical-align: inherit;">一种
         </font><font style="vertical-align: inherit;">能够存储</font><font style="vertical-align: inherit;">位值的类型。
        </font><font style="vertical-align: inherit;">范围从1到64。
      </font></font><code class="literal">BIT(<em class="replaceable"><code>M</code></em>)</code><font style="vertical-align: inherit;"></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        要指定位值，
         </font><font style="vertical-align: inherit;">可以使用表示法。</font><font style="vertical-align: inherit;">是使用零和1写的二进制值。</font><font style="vertical-align: inherit;">例如，
         </font><font style="vertical-align: inherit;">和</font><font style="vertical-align: inherit;">
        分别代表图7和128。</font><font style="vertical-align: inherit;">请参见
         </font><a class="xref" href="language-structure.html#bit-value-literals" title="9.1.5比特值文字"><font style="vertical-align: inherit;">第9.1.5节“位值文字”</font></a><font style="vertical-align: inherit;">。
      </font></font><code class="literal">b'<em class="replaceable"><code>value</code></em>'</code><font style="vertical-align: inherit;"></font><em class="replaceable"><code>value</code></em><font style="vertical-align: inherit;"></font><code class="literal">b'111'</code><font style="vertical-align: inherit;"></font><code class="literal">b'10000000'</code><font style="vertical-align: inherit;"></font><a class="xref" href="language-structure.html#bit-value-literals" title="9.1.5比特值文字"><font style="vertical-align: inherit;"></font></a><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如果</font><font style="vertical-align: inherit;">为小于</font><font style="vertical-align: inherit;">位长</font><font style="vertical-align: inherit;">的</font><font style="vertical-align: inherit;">列</font><font style="vertical-align: inherit;">分配值
         </font><font style="vertical-align: inherit;">，则会</font><font style="vertical-align: inherit;">在左侧用零填充该值。</font><font style="vertical-align: inherit;">例如，</font><font style="vertical-align: inherit;">为</font><font style="vertical-align: inherit;">
        列</font><font style="vertical-align: inherit;">分配值</font><font style="vertical-align: inherit;">实际上与分配相同
         </font><font style="vertical-align: inherit;">。
      </font></font><code class="literal">BIT(<em class="replaceable"><code>M</code></em>)</code><font style="vertical-align: inherit;"></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"></font><code class="literal">b'101'</code><font style="vertical-align: inherit;"></font><code class="literal">BIT(6)</code><font style="vertical-align: inherit;"></font><code class="literal">b'000101'</code><font style="vertical-align: inherit;"></font></p><p><b><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">NDB集群。&nbsp;</font></font></b><font style="vertical-align: inherit;"></font><code class="literal">BIT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          给定</font></font><a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表中</font><font style="vertical-align: inherit;">使用</font><font style="vertical-align: inherit;">
          的所有</font><font style="vertical-align: inherit;">列</font><font style="vertical-align: inherit;">的最大组合大小</font><font style="vertical-align: inherit;">不得超过4096位。
</font></font></p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="numeric-type-attributes"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.2.5数字类型属性</font></font></h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091693856064"></a><a class="indexterm" name="idm140091693854992"></a><a class="indexterm" name="idm140091693853920"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL支持扩展，可以选择在类型的base关键字后面的括号中指定整数数据类型的显示宽度。</font><font style="vertical-align: inherit;">例如，
         </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT(4)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">指定
         </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">显示宽度为四位的a。</font><font style="vertical-align: inherit;">应用程序可以使用此可选显示宽度来显示宽度小于为列指定的宽度的整数值，方法是用空格填充它们。</font><font style="vertical-align: inherit;">（也就是说，此宽度存在于使用结果集返回的元数据中。是否使用它取决于应用程序。）
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        显示宽度也</font></font><span class="emphasis"><em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">没有</font></font></em></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">限制，可以被存储在列中的值的范围内。</font><font style="vertical-align: inherit;">它也不会阻止比列显示宽度更宽的值正确显示。</font><font style="vertical-align: inherit;">例如，指定为
         </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">SMALLINT(3)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">具有通常
         </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">SMALLINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">范围的
         </font></font><code class="literal">-32768</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">to的列</font></font><code class="literal">32767</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，以及超过三位数允许的范围之外的值将使用超过三位数完全显示。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        与可选（非标准）</font></font><code class="literal">ZEROFILL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">属性</font><font style="vertical-align: inherit;">一起使用时
         </font><font style="vertical-align: inherit;">，默认的空格填充将替换为零。</font><font style="vertical-align: inherit;">例如，对于声明为的列</font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT(4) ZEROFILL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，将</font></font><code class="literal">5</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">检索
         </font><font style="vertical-align: inherit;">值为</font></font><code class="literal">0005</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
注意
</font></font></div>
<p>
          The <code class="literal">ZEROFILL</code> attribute is ignored for
          columns involved in expressions or
          <a class="link" href="sql-syntax.html#union" title="13.2.10.3 UNION语法"><code class="literal">UNION</code></a> queries.
        </p><p>
          If you store values larger than the display width in an
          integer column that has the <code class="literal">ZEROFILL</code>
          attribute, you may experience problems when MySQL generates
          temporary tables for some complicated joins. In these cases,
          MySQL assumes that the data values fit within the column
          display width.
</p>
</div>
<p>
        As of MySQL 8.0.17, the <code class="literal">ZEROFILL</code> attribute is
        deprecated for numeric data types, as is the display width
        attribute for integer data types. Support for
        <code class="literal">ZEROFILL</code> and display widths for integer data
        types will be removed in a future MySQL version. Consider using
        an alternative means of producing the effect of these
        attributes. For example, applications could use the
        <a class="link" href="functions.html#function_lpad"><code class="literal">LPAD()</code></a> function to zero-pad
        numbers up to the desired width, or they could store the
        formatted numbers in <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a>
        columns.
      </p><p>
        All integer types can have an optional (nonstandard)
        <code class="literal">UNSIGNED</code> attribute. An unsigned type can be
        used to permit only nonnegative numbers in a column or when you
        need a larger upper numeric range for the column. For example,
        if an <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT</code></a> column is
        <code class="literal">UNSIGNED</code>, the size of the column's range is
        the same but its endpoints shift up, from
        <code class="literal">-2147483648</code> and <code class="literal">2147483647</code>
        to <code class="literal">0</code> and <code class="literal">4294967295</code>.
      </p><p>
        Floating-point and fixed-point types also can be
        <code class="literal">UNSIGNED</code>. As with integer types, this
        attribute prevents negative values from being stored in the
        column. Unlike the integer types, the upper range of column
        values remains the same. As of MySQL 8.0.17, the
        <code class="literal">UNSIGNED</code> attribute is deprecated for columns
        of type <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a>,
        <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a>, and
        <a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a> (and any synonyms) and
        will be removed in a future MySQL version. Consider using a
        simple <code class="literal">CHECK</code> constraint instead for such
        columns.
      </p><p>
        If you specify <code class="literal">ZEROFILL</code> for a numeric column,
        MySQL automatically adds the <code class="literal">UNSIGNED</code>
        attribute.
      </p><p>
        Integer or floating-point data types can have the
        <code class="literal">AUTO_INCREMENT</code> attribute. When you insert a
        value of <code class="literal">NULL</code> into an indexed
        <code class="literal">AUTO_INCREMENT</code> column, the column is set to
        the next sequence value. Typically this is
        <code class="literal"><em class="replaceable"><code>value</code></em>+1</code>, where
        <em class="replaceable"><code>value</code></em> is the largest value for the
        column currently in the table.
        (<code class="literal">AUTO_INCREMENT</code> sequences begin with
        <code class="literal">1</code>.)
      </p><p>
        Storing <code class="literal">0</code> into an
        <code class="literal">AUTO_INCREMENT</code> column has the same effect as
        storing <code class="literal">NULL</code>, unless the
        <a class="link" href="server-administration.html#sqlmode_no_auto_value_on_zero"><code class="literal">NO_AUTO_VALUE_ON_ZERO</code></a> SQL mode
        is enabled.
      </p><p>
        Inserting <code class="literal">NULL</code> to generate
        <code class="literal">AUTO_INCREMENT</code> values requires that the
        column be declared <code class="literal">NOT NULL</code>. If the column is
        declared <code class="literal">NULL</code>, inserting
        <code class="literal">NULL</code> stores a <code class="literal">NULL</code>. When
        you insert any other value into an
        <code class="literal">AUTO_INCREMENT</code> column, the column is set to
        that value and the sequence is reset so that the next
        automatically generated value follows sequentially from the
        inserted value.
      </p><p>
        Negative values for <code class="literal">AUTO_INCREMENT</code> columns
        are not supported.
      </p><p>
        <code class="literal">CHECK</code> constraints cannot refer to columns
        that have the <code class="literal">AUTO_INCREMENT</code> attribute, nor
        can the <code class="literal">AUTO_INCREMENT</code> attribute be added to
        existing columns that are used in <code class="literal">CHECK</code>
        constraints.
      </p><p>
        As of MySQL 8.0.17, <code class="literal">AUTO_INCREMENT</code> support is
        deprecated for <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a> and
        <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a> columns and will be
        removed in a future MySQL version. Consider removing the
        <code class="literal">AUTO_INCREMENT</code> attribute from such columns,
        or convert them to an integer type.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="out-of-range-and-overflow"></a>11.2.6&nbsp;Out-of-Range and Overflow Handling</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091693790912"></a><a class="indexterm" name="idm140091693789840"></a><p>
        When MySQL stores a value in a numeric column that is outside
        the permissible range of the column data type, the result
        depends on the SQL mode in effect at the time:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            If strict SQL mode is enabled, MySQL rejects the
            out-of-range value with an error, and the insert fails, in
            accordance with the SQL standard.
          </p></li><li class="listitem"><p>
            If no restrictive modes are enabled, MySQL clips the value
            to the appropriate endpoint of the column data type range
            and stores the resulting value instead.
          </p><p>
            When an out-of-range value is assigned to an integer column,
            MySQL stores the value representing the corresponding
            endpoint of the column data type range.
          </p><p>
            When a floating-point or fixed-point column is assigned a
            value that exceeds the range implied by the specified (or
            default) precision and scale, MySQL stores the value
            representing the corresponding endpoint of that range.
</p></li></ul>
</div>
<p>
        Suppose that a table <code class="literal">t1</code> has this definition:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (i1 TINYINT, i2 TINYINT UNSIGNED);
</pre><p>
        With strict SQL mode enabled, an out of range error occurs:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SET sql_mode = 'TRADITIONAL';</code></strong>
mysql&gt; <strong class="userinput"><code>INSERT INTO t1 (i1, i2) VALUES(256, 256);</code></strong><font></font>
ERROR 1264 (22003): Out of range value for column 'i1' at row 1<font></font>
mysql&gt; <strong class="userinput"><code>SELECT * FROM t1;</code></strong><font></font>
Empty set (0.00 sec)<font></font>
</pre><p>
        With strict SQL mode not enabled, clipping with warnings occurs:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SET sql_mode = '';</code></strong>
mysql&gt; <strong class="userinput"><code>INSERT INTO t1 (i1, i2) VALUES(256, 256);</code></strong>
mysql&gt; <strong class="userinput"><code>SHOW WARNINGS;</code></strong><font></font>
+---------+------+---------------------------------------------+<font></font>
| Level   | Code | Message                                     |<font></font>
+---------+------+---------------------------------------------+<font></font>
| Warning | 1264 | Out of range value for column 'i1' at row 1 |<font></font>
| Warning | 1264 | Out of range value for column 'i2' at row 1 |<font></font>
+---------+------+---------------------------------------------+<font></font>
mysql&gt; <strong class="userinput"><code>SELECT * FROM t1;</code></strong><font></font>
+------+------+<font></font>
| i1   | i2   |<font></font>
+------+------+<font></font>
|  127 |  255 |<font></font>
+------+------+<font></font>
</pre><p>
        When strict SQL mode is not enabled, column-assignment
        conversions that occur due to clipping are reported as warnings
        for <a class="link" href="sql-syntax.html#alter-table" title="13.1.9 ALTER TABLE语法"><code class="literal">ALTER TABLE</code></a>,
        <a class="link" href="sql-syntax.html#load-data" title="13.2.7 LOAD DATA语法"><code class="literal">LOAD DATA</code></a>,
        <a class="link" href="sql-syntax.html#update" title="13.2.12 UPDATE语法"><code class="literal">UPDATE</code></a>, and multiple-row
        <a class="link" href="sql-syntax.html#insert" title="13.2.6 INSERT语法"><code class="literal">INSERT</code></a> statements. In strict
        mode, these statements fail, and some or all the values are not
        inserted or changed, depending on whether the table is a
        transactional table and other factors. For details, see
        <a class="xref" href="server-administration.html#sql-mode" title="5.1.11服务器SQL模式">Section&nbsp;5.1.11, “Server SQL Modes”</a>.
      </p><p>
        Overflow during numeric expression evaluation results in an
        error. For example, the largest signed
        <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a> value is
        9223372036854775807, so the following expression produces an
        error:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT 9223372036854775807 + 1;</code></strong><font></font>
ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'<font></font>
</pre><p>
        To enable the operation to succeed in this case, convert the
        value to unsigned;
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT CAST(9223372036854775807 AS UNSIGNED) + 1;</code></strong><font></font>
+-------------------------------------------+<font></font>
| CAST(9223372036854775807 AS UNSIGNED) + 1 |<font></font>
+-------------------------------------------+<font></font>
|                       9223372036854775808 |<font></font>
+-------------------------------------------+<font></font>
</pre><p>
        Whether overflow occurs depends on the range of the operands, so
        another way to handle the preceding expression is to use
        exact-value arithmetic because
        <a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a> values have a larger
        range than integers:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT 9223372036854775807.0 + 1;</code></strong><font></font>
+---------------------------+<font></font>
| 9223372036854775807.0 + 1 |<font></font>
+---------------------------+<font></font>
|     9223372036854775808.0 |<font></font>
+---------------------------+<font></font>
</pre><p>
        Subtraction between integer values, where one is of type
        <code class="literal">UNSIGNED</code>, produces an unsigned result by
        default. If the result would otherwise have been negative, an
        error results:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SET sql_mode = '';</code></strong><font></font>
Query OK, 0 rows affected (0.00 sec)<font></font>
<font></font>
mysql&gt; <strong class="userinput"><code>SELECT CAST(0 AS UNSIGNED) - 1;</code></strong>
<span class="errortext">ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(cast(0 as unsigned) - 1)'</span>
</pre><p>
        If the <a class="link" href="server-administration.html#sqlmode_no_unsigned_subtraction"><code class="literal">NO_UNSIGNED_SUBTRACTION</code></a>
        SQL mode is enabled, the result is negative:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT CAST(0 AS UNSIGNED) - 1;</code></strong><font></font>
+-------------------------+<font></font>
| CAST(0 AS UNSIGNED) - 1 |<font></font>
+-------------------------+<font></font>
|                      -1 |<font></font>
+-------------------------+<font></font>
</pre><p>
        If the result of such an operation is used to update an
        <code class="literal">UNSIGNED</code> integer column, the result is
        clipped to the maximum value for the column type, or clipped to
        0 if <a class="link" href="server-administration.html#sqlmode_no_unsigned_subtraction"><code class="literal">NO_UNSIGNED_SUBTRACTION</code></a>
        is enabled. If strict SQL mode is enabled, an error occurs and
        the column remains unchanged.
</p>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="date-and-time-types"></a>11.3&nbsp;Date and Time Types</h2>

</div>

</div>

</div>
<div class="toc">
<dl class="toc"><dt><span class="section"><a href="data-types.html#datetime">11.3.1 The DATE, DATETIME, and TIMESTAMP Types</a></span></dt><dt><span class="section"><a href="data-types.html#time">11.3.2 The TIME Type</a></span></dt><dt><span class="section"><a href="data-types.html#year">11.3.3 The YEAR Type</a></span></dt><dt><span class="section"><a href="data-types.html#timestamp-initialization">11.3.4 Automatic Initialization and Updating for TIMESTAMP and DATETIME</a></span></dt><dt><span class="section"><a href="data-types.html#fractional-seconds">11.3.5 Fractional Seconds in Time Values</a></span></dt><dt><span class="section"><a href="data-types.html#date-and-time-type-conversion">11.3.6 Conversion Between Date and Time Types</a></span></dt><dt><span class="section"><a href="data-types.html#two-digit-years">11.3.7 Two-Digit Years in Dates</a></span></dt></dl>
</div>
<a class="indexterm" name="idm140091693744944"></a><a class="indexterm" name="idm140091693743456"></a><p>
      The date and time types for representing temporal values are
      <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATE</code></a>,
      <a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a>,
      <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a>,
      <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a>, and
      <a class="link" href="data-types.html#year" title="11.3.3年份类型"><code class="literal">YEAR</code></a>. Each temporal type has a
      range of valid values, as well as a <span class="quote">“<span class="quote">zero</span>”</span> value that
      may be used when you specify an invalid value that MySQL cannot
      represent. The <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a> type has
      special automatic updating behavior, described later. For temporal
      type storage requirements, see
      <a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求">Section&nbsp;11.8, “Data Type Storage Requirements”</a>.
    </p><p>
      Keep in mind these general considerations when working with date
      and time types:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
          MySQL retrieves values for a given date or time type in a
          standard output format, but it attempts to interpret a variety
          of formats for input values that you supply (for example, when
          you specify a value to be assigned to or compared to a date or
          time type). For a description of the permitted formats for
          date and time types, see
          <a class="xref" href="language-structure.html#date-and-time-literals" title="9。1日期和时间文字">Section&nbsp;9.1.3, “Date and Time Literals”</a>. It is expected that
          you supply valid values. Unpredictable results may occur if
          you use values in other formats.
        </p></li><li class="listitem"><p>
          Although MySQL tries to interpret values in several formats,
          date parts must always be given in year-month-day order (for
          example, <code class="literal">'98-09-04'</code>), rather than in the
          month-day-year or day-month-year orders commonly used
          elsewhere (for example, <code class="literal">'09-04-98'</code>,
          <code class="literal">'04-09-98'</code>).
        </p></li><li class="listitem"><p>
          Dates containing two-digit year values are ambiguous because
          the century is unknown. MySQL interprets two-digit year values
          using these rules:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
              Year values in the range <code class="literal">70-99</code> are
              converted to <code class="literal">1970-1999</code>.
            </p></li><li class="listitem"><p>
              Year values in the range <code class="literal">00-69</code> are
              converted to <code class="literal">2000-2069</code>.
</p></li></ul>
</div>
<p>
          See also <a class="xref" href="data-types.html#two-digit-years" title="11.3.7&nbsp;Two-Digit Years in Dates">Section&nbsp;11.3.7, “Two-Digit Years in Dates”</a>.
        </p></li><li class="listitem"><p>
          Conversion of values from one temporal type to another occurs
          according to the rules in
          <a class="xref" href="data-types.html#date-and-time-type-conversion" title="11.3.6&nbsp;Conversion Between Date and Time Types">Section&nbsp;11.3.6, “Conversion Between Date and Time Types”</a>.
        </p></li><li class="listitem"><p>
          MySQL automatically converts a date or time value to a number
          if the value is used in a numeric context and vice versa.
        </p></li><li class="listitem"><p>
          By default, when MySQL encounters a value for a date or time
          type that is out of range or otherwise invalid for the type,
          it converts the value to the <span class="quote">“<span class="quote">zero</span>”</span> value for
          that type. The exception is that out-of-range
          <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> values are clipped to the
          appropriate endpoint of the
          <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> range.
        </p></li><li class="listitem"><p>
          By setting the SQL mode to the appropriate value, you can
          specify more exactly what kind of dates you want MySQL to
          support. (See <a class="xref" href="server-administration.html#sql-mode" title="5.1.11&nbsp;Server SQL Modes">Section&nbsp;5.1.11, “Server SQL Modes”</a>.) You can get MySQL
          to accept certain dates, such as
          <code class="literal">'2009-11-31'</code>, by enabling the
          <a class="link" href="server-administration.html#sqlmode_allow_invalid_dates"><code class="literal">ALLOW_INVALID_DATES</code></a> SQL
          mode. This is useful when you want to store a <span class="quote">“<span class="quote">possibly
          wrong</span>”</span> value which the user has specified (for example,
          in a web form) in the database for future processing. Under
          this mode, MySQL verifies only that the month is in the range
          from 1 to 12 and that the day is in the range from 1 to 31.
        </p></li><li class="listitem"><p>
          MySQL permits you to store dates where the day or month and
          day are zero in a <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a> or
          <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> column. This is useful
          for applications that need to store birthdates for which you
          may not know the exact date. In this case, you simply store
          the date as <code class="literal">'2009-00-00'</code> or
          <code class="literal">'2009-01-00'</code>. If you store dates such as
          these, you should not expect to get correct results for
          functions such as <a class="link" href="functions.html#function_date-sub"><code class="literal">DATE_SUB()</code></a> or
          <a class="link" href="functions.html#function_date-add"><code class="literal">DATE_ADD()</code></a> that require
          complete dates. To disallow zero month or day parts in dates,
          enable the <a class="link" href="server-administration.html#sqlmode_no_zero_in_date"><code class="literal">NO_ZERO_IN_DATE</code></a>
          mode.
        </p></li><li class="listitem"><p>
          MySQL permits you to store a <span class="quote">“<span class="quote">zero</span>”</span> value of
          <code class="literal">'0000-00-00'</code> as a <span class="quote">“<span class="quote">dummy
          date.</span>”</span> This is in some cases more convenient than using
          <code class="literal">NULL</code> values, and uses less data and index
          space. To disallow <code class="literal">'0000-00-00'</code>, enable the
          <a class="link" href="server-administration.html#sqlmode_no_zero_date"><code class="literal">NO_ZERO_DATE</code></a> mode.
        </p></li><li class="listitem"><p>
          <span class="quote">“<span class="quote">Zero</span>”</span> date or time values used through
          Connector/ODBC are converted automatically to
          <code class="literal">NULL</code> because ODBC cannot handle such
          values.
</p></li></ul>
</div>
<p>
      The following table shows the format of the <span class="quote">“<span class="quote">zero</span>”</span>
      value for each type. The <span class="quote">“<span class="quote">zero</span>”</span> values are special,
      but you can store or refer to them explicitly using the values
      shown in the table. You can also do this using the values
      <code class="literal">'0'</code> or <code class="literal">0</code>, which are easier
      to write. For temporal types that include a date part
      (<a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a>,
      <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a>, and
      <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a>), use of these values
      produces warnings if the
      <a class="link" href="server-administration.html#sqlmode_no_zero_date"><code class="literal">NO_ZERO_DATE</code></a> SQL mode is
      enabled.
</p>
<div class="informaltable">
<table summary="Format of the zero value for temporal data types."><colgroup><col width="30%"><col width="40%"></colgroup><thead><tr>
          <th scope="col">Data Type</th>
          <th scope="col"><span class="quote">“<span class="quote">Zero</span>”</span> Value</th>
        </tr></thead><tbody><tr>
          <td scope="row"><a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a></td>
          <td><code class="literal">'0000-00-00'</code></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a></td>
          <td><code class="literal">'00:00:00'</code></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a></td>
          <td><code class="literal">'0000-00-00 00:00:00'</code></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a></td>
          <td><code class="literal">'0000-00-00 00:00:00'</code></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#year" title="11.3.3&nbsp;The YEAR Type"><code class="literal">YEAR</code></a></td>
          <td><code class="literal">0000</code></td>
</tr></tbody></table>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="datetime"></a>11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091693659072"></a><a class="indexterm" name="idm140091693658032"></a><a class="indexterm" name="idm140091693656960"></a><a class="indexterm" name="idm140091693655888"></a><a class="indexterm" name="idm140091693654400"></a><a class="indexterm" name="idm140091693652912"></a><p>
        The <code class="literal">DATE</code>, <code class="literal">DATETIME</code>, and
        <code class="literal">TIMESTAMP</code> types are related. This section
        describes their characteristics, how they are similar, and how
        they differ. MySQL recognizes <code class="literal">DATE</code>,
        <code class="literal">DATETIME</code>, and <code class="literal">TIMESTAMP</code>
        values in several formats, described in
        <a class="xref" href="language-structure.html#date-and-time-literals" title="9.1.3&nbsp;Date and Time Literals">Section&nbsp;9.1.3, “Date and Time Literals”</a>. For the
        <code class="literal">DATE</code> and <code class="literal">DATETIME</code> range
        descriptions, <span class="quote">“<span class="quote">supported</span>”</span> means that although
        earlier values might work, there is no guarantee.
      </p><p>
        The <code class="literal">DATE</code> type is used for values with a date
        part but no time part. MySQL retrieves and displays
        <code class="literal">DATE</code> values in
        <code class="literal">'YYYY-MM-DD'</code> format. The supported range is
        <code class="literal">'1000-01-01'</code> to
        <code class="literal">'9999-12-31'</code>.
      </p><p>
        The <code class="literal">DATETIME</code> type is used for values that
        contain both date and time parts. MySQL retrieves and displays
        <code class="literal">DATETIME</code> values in <em class="replaceable"><code>'YYYY-MM-DD
        hh:mm:ss'</code></em> format. The supported range is
        <code class="literal">'1000-01-01 00:00:00'</code> to <code class="literal">'9999-12-31
        23:59:59'</code>.
      </p><p>
        The <code class="literal">TIMESTAMP</code> data type is used for values
        that contain both date and time parts.
        <code class="literal">TIMESTAMP</code> has a range of <code class="literal">'1970-01-01
        00:00:01'</code> UTC to <code class="literal">'2038-01-19
        03:14:07'</code> UTC.
      </p><p>
        A <code class="literal">DATETIME</code> or <code class="literal">TIMESTAMP</code>
        value can include a trailing fractional seconds part in up to
        microseconds (6 digits) precision. In particular, any fractional
        part in a value inserted into a <code class="literal">DATETIME</code> or
        <code class="literal">TIMESTAMP</code> column is stored rather than
        discarded. With the fractional part included, the format for
        these values is <code class="literal">'<em class="replaceable"><code>YYYY-MM-DD
        hh:mm:ss</code></em>[.<em class="replaceable"><code>fraction</code></em>]'</code>,
        the range for <code class="literal">DATETIME</code> values is
        <code class="literal">'1000-01-01 00:00:00.000000'</code> to
        <code class="literal">'9999-12-31 23:59:59.999999'</code>, and the range
        for <code class="literal">TIMESTAMP</code> values is <code class="literal">'1970-01-01
        00:00:01.000000'</code> to <code class="literal">'2038-01-19
        03:14:07.999999'</code>. The fractional part should always be
        separated from the rest of the time by a decimal point; no other
        fractional seconds delimiter is recognized. For information
        about fractional seconds support in MySQL, see
        <a class="xref" href="data-types.html#fractional-seconds" title="11.3.5&nbsp;Fractional Seconds in Time Values">Section&nbsp;11.3.5, “Fractional Seconds in Time Values”</a>.
      </p><p>
        The <code class="literal">TIMESTAMP</code> and <code class="literal">DATETIME</code>
        data types offer automatic initialization and updating to the
        current date and time. For more information, see
        <a class="xref" href="data-types.html#timestamp-initialization" title="11.3.4&nbsp;Automatic Initialization and Updating for TIMESTAMP and DATETIME">Section&nbsp;11.3.4, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”</a>.
      </p><p>
        MySQL converts <code class="literal">TIMESTAMP</code> values from the
        current time zone to UTC for storage, and back from UTC to the
        current time zone for retrieval. (This does not occur for other
        types such as <code class="literal">DATETIME</code>.) By default, the
        current time zone for each connection is the server's time. The
        time zone can be set on a per-connection basis. As long as the
        time zone setting remains constant, you get back the same value
        you store. If you store a <code class="literal">TIMESTAMP</code> value,
        and then change the time zone and retrieve the value, the
        retrieved value is different from the value you stored. This
        occurs because the same time zone was not used for conversion in
        both directions. The current time zone is available as the value
        of the <a class="link" href="server-administration.html#sysvar_time_zone"><code class="literal">time_zone</code></a> system
        variable. For more information, see
        <a class="xref" href="server-administration.html#time-zone-support" title="5.1.13&nbsp;MySQL Server Time Zone Support">Section&nbsp;5.1.13, “MySQL Server Time Zone Support”</a>.
      </p><p>
        Invalid <code class="literal">DATE</code>, <code class="literal">DATETIME</code>, or
        <code class="literal">TIMESTAMP</code> values are converted to the
        <span class="quote">“<span class="quote">zero</span>”</span> value of the appropriate type
        (<code class="literal">'0000-00-00'</code> or <code class="literal">'0000-00-00
        00:00:00'</code>).
      </p><a class="indexterm" name="idm140091693609600"></a><a class="indexterm" name="idm140091693608112"></a><p>
        Be aware of certain properties of date value interpretation in
        MySQL:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            MySQL permits a <span class="quote">“<span class="quote">relaxed</span>”</span> format for values
            specified as strings, in which any punctuation character may
            be used as the delimiter between date parts or time parts.
            In some cases, this syntax can be deceiving. For example, a
            value such as <code class="literal">'10:11:12'</code> might look like
            a time value because of the <code class="literal">:</code>, but is
            interpreted as the year <code class="literal">'2010-11-12'</code> if
            used in a date context. The value
            <code class="literal">'10:45:15'</code> is converted to
            <code class="literal">'0000-00-00'</code> because
            <code class="literal">'45'</code> is not a valid month.
          </p><p>
            The only delimiter recognized between a date and time part
            and a fractional seconds part is the decimal point.
          </p></li><li class="listitem"><p>
            The server requires that month and day values be valid, and
            not merely in the range 1 to 12 and 1 to 31, respectively.
            With strict mode disabled, invalid dates such as
            <code class="literal">'2004-04-31'</code> are converted to
            <code class="literal">'0000-00-00'</code> and a warning is generated.
            With strict mode enabled, invalid dates generate an error.
            To permit such dates, enable
            <a class="link" href="server-administration.html#sqlmode_allow_invalid_dates"><code class="literal">ALLOW_INVALID_DATES</code></a>. See
            <a class="xref" href="server-administration.html#sql-mode" title="5.1.11&nbsp;Server SQL Modes">Section&nbsp;5.1.11, “Server SQL Modes”</a>, for more information.
          </p></li><li class="listitem"><p>
            MySQL does not accept <code class="literal">TIMESTAMP</code> values
            that include a zero in the day or month column or values
            that are not a valid date. The sole exception to this rule
            is the special <span class="quote">“<span class="quote">zero</span>”</span> value
            <code class="literal">'0000-00-00 00:00:00'</code>.
          </p></li><li class="listitem"><p>
            Dates containing two-digit year values are ambiguous because
            the century is unknown. MySQL interprets two-digit year
            values using these rules:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                Year values in the range <code class="literal">00-69</code> are
                converted to <code class="literal">2000-2069</code>.
              </p></li><li class="listitem"><p>
                Year values in the range <code class="literal">70-99</code> are
                converted to <code class="literal">1970-1999</code>.
</p></li></ul>
</div>
<p>
            See also <a class="xref" href="data-types.html#two-digit-years" title="11.3.7&nbsp;Two-Digit Years in Dates">Section&nbsp;11.3.7, “Two-Digit Years in Dates”</a>.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="time"></a>11.3.2&nbsp;The TIME Type</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091693583568"></a><a class="indexterm" name="idm140091693582496"></a><p>
        MySQL retrieves and displays <code class="literal">TIME</code> values in
        <em class="replaceable"><code>'hh:mm:ss'</code></em> format (or
        <em class="replaceable"><code>'hhh:mm:ss'</code></em> format for large hours
        values). <code class="literal">TIME</code> values may range from
        <code class="literal">'-838:59:59'</code> to
        <code class="literal">'838:59:59'</code>. The hours part may be so large
        because the <code class="literal">TIME</code> type can be used not only to
        represent a time of day (which must be less than 24 hours), but
        also elapsed time or a time interval between two events (which
        may be much greater than 24 hours, or even negative).
      </p><p>
        MySQL recognizes <code class="literal">TIME</code> values in several
        formats, some of which can include a trailing fractional seconds
        part in up to microseconds (6 digits) precision. See
        <a class="xref" href="language-structure.html#date-and-time-literals" title="9.1.3&nbsp;Date and Time Literals">Section&nbsp;9.1.3, “Date and Time Literals”</a>. For information about
        fractional seconds support in MySQL, see
        <a class="xref" href="data-types.html#fractional-seconds" title="11.3.5&nbsp;Fractional Seconds in Time Values">Section&nbsp;11.3.5, “Fractional Seconds in Time Values”</a>. In particular, any
        fractional part in a value inserted into a
        <code class="literal">TIME</code> column is stored rather than discarded.
        With the fractional part included, the range for
        <code class="literal">TIME</code> values is
        <code class="literal">'-838:59:59.000000'</code> to
        <code class="literal">'838:59:59.000000'</code>.
      </p><p>
        Be careful about assigning abbreviated values to a
        <code class="literal">TIME</code> column. MySQL interprets abbreviated
        <code class="literal">TIME</code> values with colons as time of the day.
        That is, <code class="literal">'11:12'</code> means
        <code class="literal">'11:12:00'</code>, not
        <code class="literal">'00:11:12'</code>. MySQL interprets abbreviated
        values without colons using the assumption that the two
        rightmost digits represent seconds (that is, as elapsed time
        rather than as time of day). For example, you might think of
        <code class="literal">'1112'</code> and <code class="literal">1112</code> as meaning
        <code class="literal">'11:12:00'</code> (12 minutes after 11 o'clock), but
        MySQL interprets them as <code class="literal">'00:11:12'</code> (11
        minutes, 12 seconds). Similarly, <code class="literal">'12'</code> and
        <code class="literal">12</code> are interpreted as
        <code class="literal">'00:00:12'</code>.
      </p><p>
        The only delimiter recognized between a time part and a
        fractional seconds part is the decimal point.
      </p><p>
        By default, values that lie outside the <code class="literal">TIME</code>
        range but are otherwise valid are clipped to the closest
        endpoint of the range. For example,
        <code class="literal">'-850:00:00'</code> and
        <code class="literal">'850:00:00'</code> are converted to
        <code class="literal">'-838:59:59'</code> and
        <code class="literal">'838:59:59'</code>. Invalid <code class="literal">TIME</code>
        values are converted to <code class="literal">'00:00:00'</code>. Note that
        because <code class="literal">'00:00:00'</code> is itself a valid
        <code class="literal">TIME</code> value, there is no way to tell, from a
        value of <code class="literal">'00:00:00'</code> stored in a table,
        whether the original value was specified as
        <code class="literal">'00:00:00'</code> or whether it was invalid.
      </p><p>
        For more restrictive treatment of invalid
        <code class="literal">TIME</code> values, enable strict SQL mode to cause
        errors to occur. See <a class="xref" href="server-administration.html#sql-mode" title="5.1.11&nbsp;Server SQL Modes">Section&nbsp;5.1.11, “Server SQL Modes”</a>.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="year"></a>11.3.3&nbsp;The YEAR Type</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091693548304"></a><a class="indexterm" name="idm140091693547232"></a><p>
        The <code class="literal">YEAR</code> type is a 1-byte type used to
        represent year values. It can be declared as
        <code class="literal">YEAR</code> or <code class="literal">YEAR(4)</code> and has a
        display width of four characters.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title">
Note
</div>
<p>
          MySQL 8.0 does not support the
          <a class="link" href="data-types.html#year" title="11.3.3&nbsp;The YEAR Type"><code class="literal">YEAR(2)</code></a> data type permitted in
          older versions of MySQL. For instructions on converting to
          <a class="link" href="data-types.html#year" title="11.3.3&nbsp;The YEAR Type"><code class="literal">YEAR(4)</code></a>, see
          <a class="ulink" href="https://dev.mysql.com/doc/refman/5.7/en/migrating-to-year4.html" target="_top">YEAR(2) Limitations and Migrating to YEAR(4)</a> in
          <a class="ulink" href="https://dev.mysql.com/doc/refman/5.7/en/" target="_top">MySQL 5.7 Reference Manual</a>.
</p>
</div>
<p>
        MySQL displays <code class="literal">YEAR</code> values in
        <code class="literal">YYYY</code> format, with a range of
        <code class="literal">1901</code> to <code class="literal">2155</code>, or
        <code class="literal">0000</code>.
      </p><p>
        You can specify input <code class="literal">YEAR</code> values in a
        variety of formats:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            As a 4-digit number in the range <code class="literal">1901</code> to
            <code class="literal">2155</code>.
          </p></li><li class="listitem"><p>
            As a 4-digit string in the range <code class="literal">'1901'</code>
            to <code class="literal">'2155'</code>.
          </p></li><li class="listitem"><p>
            As a 1- or 2-digit number in the range <code class="literal">1</code>
            to <code class="literal">99</code>. MySQL converts values in the
            ranges <code class="literal">1</code> to <code class="literal">69</code> and
            <code class="literal">70</code> to <code class="literal">99</code> to
            <code class="literal">YEAR</code> values in the ranges
            <code class="literal">2001</code> to <code class="literal">2069</code> and
            <code class="literal">1970</code> to <code class="literal">1999</code>.
          </p></li><li class="listitem"><p>
            As a 1- or 2-digit string in the range
            <code class="literal">'0'</code> to <code class="literal">'99'</code>. MySQL
            converts values in the ranges <code class="literal">'0'</code> to
            <code class="literal">'69'</code> and <code class="literal">'70'</code> to
            <code class="literal">'99'</code> to <code class="literal">YEAR</code> values in
            the ranges <code class="literal">2000</code> to
            <code class="literal">2069</code> and <code class="literal">1970</code> to
            <code class="literal">1999</code>.
          </p></li><li class="listitem"><p>
            The result of inserting a numeric <code class="literal">0</code> has a
            display value of <code class="literal">0000</code> and an internal
            value of <code class="literal">0000</code>. To insert zero and have it
            be interpreted as <code class="literal">2000</code>, specify it as a
            string <code class="literal">'0'</code> or <code class="literal">'00'</code>.
          </p></li><li class="listitem"><p>
            As the result of a function that returns a value that is
            acceptable in a <code class="literal">YEAR</code> context, such as
            <a class="link" href="functions.html#function_now"><code class="literal">NOW()</code></a>.
</p></li></ul>
</div>
<p>
        MySQL converts invalid <code class="literal">YEAR</code> values to
        <code class="literal">0000</code>.
      </p><p>
        See also <a class="xref" href="data-types.html#two-digit-years" title="11.3.7&nbsp;Two-Digit Years in Dates">Section&nbsp;11.3.7, “Two-Digit Years in Dates”</a>.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="timestamp-initialization"></a>11.3.4&nbsp;Automatic Initialization and Updating for TIMESTAMP and DATETIME</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091693498176"></a><p>
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> and
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> columns can be
        automatically initializated and updated to the current date and
        time (that is, the current timestamp).
      </p><p>
        For any <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> or
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> column in a table, you
        can assign the current timestamp as the default value, the
        auto-update value, or both:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            An auto-initialized column is set to the current timestamp
            for inserted rows that specify no value for the column.
          </p></li><li class="listitem"><p>
            An auto-updated column is automatically updated to the
            current timestamp when the value of any other column in the
            row is changed from its current value. An auto-updated
            column remains unchanged if all other columns are set to
            their current values. To prevent an auto-updated column from
            updating when other columns change, explicitly set it to its
            current value. To update an auto-updated column even when
            other columns do not change, explicitly set it to the value
            it should have (for example, set it to
            <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a>).
</p></li></ul>
</div>
<p>
        In addition, if the
        <a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a>
        system variable is disabled, you can initialize or update any
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> (but not
        <code class="literal">DATETIME</code>) column to the current date and time
        by assigning it a <code class="literal">NULL</code> value, unless it has
        been defined with the <code class="literal">NULL</code> attribute to
        permit <code class="literal">NULL</code> values.
      </p><p>
        To specify automatic properties, use the <code class="literal">DEFAULT
        CURRENT_TIMESTAMP</code> and <code class="literal">ON UPDATE
        CURRENT_TIMESTAMP</code> clauses in column definitions. The
        order of the clauses does not matter. If both are present in a
        column definition, either can occur first. Any of the synonyms
        for <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a> have the
        same meaning as
        <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a>. These are
        <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP()</code></a>,
        <a class="link" href="functions.html#function_now"><code class="literal">NOW()</code></a>,
        <a class="link" href="functions.html#function_localtime"><code class="literal">LOCALTIME</code></a>,
        <a class="link" href="functions.html#function_localtime"><code class="literal">LOCALTIME()</code></a>,
        <a class="link" href="functions.html#function_localtimestamp"><code class="literal">LOCALTIMESTAMP</code></a>, and
        <a class="link" href="functions.html#function_localtimestamp"><code class="literal">LOCALTIMESTAMP()</code></a>.
      </p><p>
        Use of <code class="literal">DEFAULT CURRENT_TIMESTAMP</code> and
        <code class="literal">ON UPDATE CURRENT_TIMESTAMP</code> is specific to
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> and
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a>. The
        <code class="literal">DEFAULT</code> clause also can be used to specify a
        constant (nonautomatic) default value (for example,
        <code class="literal">DEFAULT 0</code> or <code class="literal">DEFAULT '2000-01-01
        00:00:00'</code>).
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title">
Note
</div>
<p>
          The following examples use <code class="literal">DEFAULT 0</code>, a
          default that can produce warnings or errors depending on
          whether strict SQL mode or the
          <a class="link" href="server-administration.html#sqlmode_no_zero_date"><code class="literal">NO_ZERO_DATE</code></a> SQL mode is
          enabled. Be aware that the
          <a class="link" href="server-administration.html#sqlmode_traditional"><code class="literal">TRADITIONAL</code></a> SQL mode
          includes strict mode and
          <a class="link" href="server-administration.html#sqlmode_no_zero_date"><code class="literal">NO_ZERO_DATE</code></a>. See
          <a class="xref" href="server-administration.html#sql-mode" title="5.1.11&nbsp;Server SQL Modes">Section&nbsp;5.1.11, “Server SQL Modes”</a>.
</p>
</div>
<p>
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> or
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> column definitions can
        specify the current timestamp for both the default and
        auto-update values, for one but not the other, or for neither.
        Different columns can have different combinations of automatic
        properties. The following rules describe the possibilities:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            With both <code class="literal">DEFAULT CURRENT_TIMESTAMP</code> and
            <code class="literal">ON UPDATE CURRENT_TIMESTAMP</code>, the column
            has the current timestamp for its default value and is
            automatically updated to the current timestamp.
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,<font></font>
  dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP<font></font>
);<font></font>
</pre></li><li class="listitem"><p>
            With a <code class="literal">DEFAULT</code> clause but no <code class="literal">ON
            UPDATE CURRENT_TIMESTAMP</code> clause, the column has
            the given default value and is not automatically updated to
            the current timestamp.
          </p><p>
            The default depends on whether the
            <code class="literal">DEFAULT</code> clause specifies
            <code class="literal">CURRENT_TIMESTAMP</code> or a constant value.
            With <code class="literal">CURRENT_TIMESTAMP</code>, the default is
            the current timestamp.
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,<font></font>
  dt DATETIME DEFAULT CURRENT_TIMESTAMP<font></font>
);<font></font>
</pre><p>
            With a constant, the default is the given value. In this
            case, the column has no automatic properties at all.
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  ts TIMESTAMP DEFAULT 0,<font></font>
  dt DATETIME DEFAULT 0<font></font>
);<font></font>
</pre></li><li class="listitem"><p>
            With an <code class="literal">ON UPDATE CURRENT_TIMESTAMP</code>
            clause and a constant <code class="literal">DEFAULT</code> clause, the
            column is automatically updated to the current timestamp and
            has the given constant default value.
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP,<font></font>
  dt DATETIME DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP<font></font>
);<font></font>
</pre></li><li class="listitem"><p>
            With an <code class="literal">ON UPDATE CURRENT_TIMESTAMP</code>
            clause but no <code class="literal">DEFAULT</code> clause, the column
            is automatically updated to the current timestamp but does
            not have the current timestamp for its default value.
          </p><p>
            The default in this case is type dependent.
            <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> has a default of 0
            unless defined with the <code class="literal">NULL</code> attribute,
            in which case the default is <code class="literal">NULL</code>.
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  ts1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,     -- default 0<font></font>
  ts2 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL<font></font>
);<font></font>
</pre><p>
            <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> has a default of
            <code class="literal">NULL</code> unless defined with the <code class="literal">NOT
            NULL</code> attribute, in which case the default is 0.
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  dt1 DATETIME ON UPDATE CURRENT_TIMESTAMP,         -- default NULL<font></font>
  dt2 DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP -- default 0<font></font>
);<font></font>
</pre></li></ul>
</div>
<p>
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> and
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> columns have no
        automatic properties unless they are specified explicitly, with
        this exception: If the
        <a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a>
        system variable is disabled, the <span class="emphasis"><em>first</em></span>
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> column has both
        <code class="literal">DEFAULT CURRENT_TIMESTAMP</code> and <code class="literal">ON
        UPDATE CURRENT_TIMESTAMP</code> if neither is specified
        explicitly. To suppress automatic properties for the first
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> column, use one of
        these strategies:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Enable the
            <a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a>
            system variable. In this case, the <code class="literal">DEFAULT
            CURRENT_TIMESTAMP</code> and <code class="literal">ON UPDATE
            CURRENT_TIMESTAMP</code> clauses that specify automatic
            initialization and updating are available, but are not
            assigned to any <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a>
            column unless explicitly included in the column definition.
          </p></li><li class="listitem"><p>
            Alternatively, if
            <a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a>
            is disabled, do either of the following:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                Define the column with a <code class="literal">DEFAULT</code>
                clause that specifies a constant default value.
              </p></li><li class="listitem"><p>
                Specify the <code class="literal">NULL</code> attribute. This also
                causes the column to permit <code class="literal">NULL</code>
                values, which means that you cannot assign the current
                timestamp by setting the column to
                <code class="literal">NULL</code>. Assigning
                <code class="literal">NULL</code> sets the column to
                <code class="literal">NULL</code>, not the current timestamp. To
                assign the current timestamp, set the column to
                <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a> or a
                synonym such as <a class="link" href="functions.html#function_now"><code class="literal">NOW()</code></a>.
</p></li></ul>
</div>
</li></ul>
</div>
<p>
        Consider these table definitions:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  ts1 TIMESTAMP DEFAULT 0,<font></font>
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP<font></font>
                ON UPDATE CURRENT_TIMESTAMP);<font></font>
CREATE TABLE t2 (<font></font>
  ts1 TIMESTAMP NULL,<font></font>
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP<font></font>
                ON UPDATE CURRENT_TIMESTAMP);<font></font>
CREATE TABLE t3 (<font></font>
  ts1 TIMESTAMP NULL DEFAULT 0,<font></font>
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP<font></font>
                ON UPDATE CURRENT_TIMESTAMP);<font></font>
</pre><p>
        The tables have these properties:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            In each table definition, the first
            <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> column has no
            automatic initialization or updating.
          </p></li><li class="listitem"><p>
            The tables differ in how the <code class="literal">ts1</code> column
            handles <code class="literal">NULL</code> values. For
            <code class="literal">t1</code>, <code class="literal">ts1</code> is
            <code class="literal">NOT NULL</code> and assigning it a value of
            <code class="literal">NULL</code> sets it to the current timestamp.
            For <code class="literal">t2</code> and <code class="literal">t3</code>,
            <code class="literal">ts1</code> permits <code class="literal">NULL</code> and
            assigning it a value of <code class="literal">NULL</code> sets it to
            <code class="literal">NULL</code>.
          </p></li><li class="listitem"><p>
            <code class="literal">t2</code> and <code class="literal">t3</code> differ in
            the default value for <code class="literal">ts1</code>. For
            <code class="literal">t2</code>, <code class="literal">ts1</code> is defined to
            permit <code class="literal">NULL</code>, so the default is also
            <code class="literal">NULL</code> in the absence of an explicit
            <code class="literal">DEFAULT</code> clause. For
            <code class="literal">t3</code>, <code class="literal">ts1</code> permits
            <code class="literal">NULL</code> but has an explicit default of 0.
</p></li></ul>
</div>
<p>
        If a <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> or
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> column definition
        includes an explicit fractional seconds precision value
        anywhere, the same value must be used throughout the column
        definition. This is permitted:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)<font></font>
);<font></font>
</pre><p>
        This is not permitted:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(3)<font></font>
);<font></font>
</pre>
<h4><a name="idm140091693372896"></a>TIMESTAMP Initialization and the NULL Attribute</h4>
<p>
        If the
        <a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a>
        system variable is disabled,
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> columns by default are
        <code class="literal">NOT NULL</code>, cannot contain
        <code class="literal">NULL</code> values, and assigning
        <code class="literal">NULL</code> assigns the current timestamp. To permit
        a <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> column to contain
        <code class="literal">NULL</code>, explicitly declare it with the
        <code class="literal">NULL</code> attribute. In this case, the default
        value also becomes <code class="literal">NULL</code> unless overridden
        with a <code class="literal">DEFAULT</code> clause that specifies a
        different default value. <code class="literal">DEFAULT NULL</code> can be
        used to explicitly specify <code class="literal">NULL</code> as the
        default value. (For a <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a>
        column not declared with the <code class="literal">NULL</code> attribute,
        <code class="literal">DEFAULT NULL</code> is invalid.) If a
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> column permits
        <code class="literal">NULL</code> values, assigning
        <code class="literal">NULL</code> sets it to <code class="literal">NULL</code>, not
        to the current timestamp.
      </p><p>
        The following table contains several
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> columns that permit
        <code class="literal">NULL</code> values:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t<font></font>
(<font></font>
  ts1 TIMESTAMP NULL DEFAULT NULL,<font></font>
  ts2 TIMESTAMP NULL DEFAULT 0,<font></font>
  ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP<font></font>
);<font></font>
</pre><p>
        A <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> column that permits
        <code class="literal">NULL</code> values does <span class="emphasis"><em>not</em></span>
        take on the current timestamp at insert time except under one of
        the following conditions:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Its default value is defined as
            <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a> and no
            value is specified for the column
          </p></li><li class="listitem"><p>
            <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a> or any of
            its synonyms such as <a class="link" href="functions.html#function_now"><code class="literal">NOW()</code></a> is
            explicitly inserted into the column
</p></li></ul>
</div>
<p>
        In other words, a <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a>
        column defined to permit <code class="literal">NULL</code> values
        auto-initializes only if its definition includes
        <code class="literal">DEFAULT CURRENT_TIMESTAMP</code>:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t (ts TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);
</pre><p>
        If the <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> column permits
        <code class="literal">NULL</code> values but its definition does not
        include <code class="literal">DEFAULT CURRENT_TIMESTAMP</code>, you must
        explicitly insert a value corresponding to the current date and
        time. Suppose that tables <code class="literal">t1</code> and
        <code class="literal">t2</code> have these definitions:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (ts TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00');<font></font>
CREATE TABLE t2 (ts TIMESTAMP NULL DEFAULT NULL);<font></font>
</pre><p>
        To set the <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> column in
        either table to the current timestamp at insert time, explicitly
        assign it that value. For example:
      </p><pre data-lang="sql" class="programlisting">INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);<font></font>
INSERT INTO t1 VALUES (NOW());<font></font>
</pre><p>
        If the
        <a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a>
        system variable is enabled,
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> columns permit
        <code class="literal">NULL</code> values only if declared with the
        <code class="literal">NULL</code> attribute. Also,
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> columns do not permit
        assigning <code class="literal">NULL</code> to assign the current
        timestamp, whether declared with the <code class="literal">NULL</code> or
        <code class="literal">NOT NULL</code> attribute. To assign the current
        timestamp, set the column to
        <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a> or a synonym
        such as <a class="link" href="functions.html#function_now"><code class="literal">NOW()</code></a>.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="fractional-seconds"></a>11.3.5&nbsp;Fractional Seconds in Time Values</h3>

</div>

</div>

</div>
<p>
        MySQL 8.0 has fractional seconds support for
        <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a>,
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a>, and
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> values, with up to
        microseconds (6 digits) precision:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            To define a column that includes a fractional seconds part,
            use the syntax
            <code class="literal"><em class="replaceable"><code>type_name</code></em>(<em class="replaceable"><code>fsp</code></em>)</code>,
            where <em class="replaceable"><code>type_name</code></em> is
            <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a>,
            <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a>, or
            <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a>, and
            <em class="replaceable"><code>fsp</code></em> is the fractional seconds
            precision. For example:
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (t TIME(3), dt DATETIME(6));
</pre><p>
            The <em class="replaceable"><code>fsp</code></em> value, if given, must be
            in the range 0 to 6. A value of 0 signifies that there is no
            fractional part. If omitted, the default precision is 0.
            (This differs from the standard SQL default of 6, for
            compatibility with previous MySQL versions.)
          </p></li><li class="listitem"><p>
            Inserting a <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a>,
            <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a>, or
            <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> value with a
            fractional seconds part into a column of the same type but
            having fewer fractional digits results in rounding. Consider
            a table created and populated as follows:
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE fractest( c1 TIME(2), c2 DATETIME(2), c3 TIMESTAMP(2) );<font></font>
INSERT INTO fractest VALUES<font></font>
('17:51:04.777', '2018-09-08 17:51:04.777', '2018-09-08 17:51:04.777');<font></font>
</pre><p>
            The temporal values are inserted into the table with
            rounding:
          </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT * FROM fractest;</code></strong><font></font>
+-------------+------------------------+------------------------+<font></font>
| c1          | c2                     | c3                     |<font></font>
+-------------+------------------------+------------------------+<font></font>
| 17:51:04.78 | 2018-09-08 17:51:04.78 | 2018-09-08 17:51:04.78 |<font></font>
+-------------+------------------------+------------------------+<font></font>
</pre><p>
            No warning or error is given when such rounding occurs. This
            behavior follows the SQL standard.
          </p><p>
            To insert the values with truncation instead, enable the
            <a class="link" href="server-administration.html#sqlmode_time_truncate_fractional"><code class="literal">TIME_TRUNCATE_FRACTIONAL</code></a>
            SQL mode:
          </p><pre data-lang="sql" class="programlisting">SET @@sql_mode = sys.list_add(@@sql_mode, 'TIME_TRUNCATE_FRACTIONAL');
</pre><p>
            With that SQL mode enabled, the temporal values are inserted
            with truncation:
          </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT * FROM fractest;</code></strong><font></font>
+-------------+------------------------+------------------------+<font></font>
| c1          | c2                     | c3                     |<font></font>
+-------------+------------------------+------------------------+<font></font>
| 17:51:04.77 | 2018-09-08 17:51:04.77 | 2018-09-08 17:51:04.77 |<font></font>
+-------------+------------------------+------------------------+<font></font>
</pre></li><li class="listitem"><p>
            Functions that take temporal arguments accept values with
            fractional seconds. Return values from temporal functions
            include fractional seconds as appropriate. For example,
            <a class="link" href="functions.html#function_now"><code class="literal">NOW()</code></a> with no argument
            returns the current date and time with no fractional part,
            but takes an optional argument from 0 to 6 to specify that
            the return value includes a fractional seconds part of that
            many digits.
          </p></li><li class="listitem"><p>
            Syntax for temporal literals produces temporal values:
            <code class="literal">DATE '<em class="replaceable"><code>str</code></em>'</code>,
            <code class="literal">TIME '<em class="replaceable"><code>str</code></em>'</code>,
            and <code class="literal">TIMESTAMP
            '<em class="replaceable"><code>str</code></em>'</code>, and the
            ODBC-syntax equivalents. The resulting value includes a
            trailing fractional seconds part if specified. Previously,
            the temporal type keyword was ignored and these constructs
            produced the string value. See
            <a class="xref" href="language-structure.html#date-and-time-standard-sql-literals" title="Standard SQL and ODBC Date and Time Literals">Standard SQL and ODBC Date and Time Literals</a>
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="date-and-time-type-conversion"></a>11.3.6&nbsp;Conversion Between Date and Time Types</h3>

</div>

</div>

</div>
<p>
        To some extent, you can convert a value from one temporal type
        to another. However, there may be some alteration of the value
        or loss of information. In all cases, conversion between
        temporal types is subject to the range of valid values for the
        resulting type. For example, although
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a>,
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a>, and
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> values all can be
        specified using the same set of formats, the types do not all
        have the same range of values.
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> values cannot be
        earlier than <code class="literal">1970</code> UTC or later than
        <code class="literal">'2038-01-19 03:14:07'</code> UTC. This means that a
        date such as <code class="literal">'1968-01-01'</code>, while valid as a
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a> or
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> value, is not valid as a
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> value and is converted
        to <code class="literal">0</code>.
      </p><p>
        Conversion of <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a> values:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Conversion to a <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> or
            <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> value adds a time
            part of <code class="literal">'00:00:00'</code> because the
            <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a> value contains no time
            information.
          </p></li><li class="listitem"><p>
            Conversion to a <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> value is
            not useful; the result is <code class="literal">'00:00:00'</code>.
</p></li></ul>
</div>
<p>
        Conversion of <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> and
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> values:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Conversion to a <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a> value
            takes fractional seconds into account and rounds the time
            part. For example, <code class="literal">'1999-12-31
            23:59:59.499'</code> becomes
            <code class="literal">'1999-12-31'</code>, whereas
            <code class="literal">'1999-12-31 23:59:59.500'</code> becomes
            <code class="literal">'2000-01-01'</code>.
          </p></li><li class="listitem"><p>
            Conversion to a <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> value
            discards the date part because the
            <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> type contains no date
            information.
</p></li></ul>
</div>
<p>
        For conversion of <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> values to
        other temporal types, the value of
        <a class="link" href="functions.html#function_current-date"><code class="literal">CURRENT_DATE()</code></a> is used for the
        date part. The <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> is
        interpreted as elapsed time (not time of day) and added to the
        date. This means that the date part of the result differs from
        the current date if the time value is outside the range from
        <code class="literal">'00:00:00'</code> to <code class="literal">'23:59:59'</code>.
      </p><p>
        Suppose that the current date is
        <code class="literal">'2012-01-01'</code>.
        <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> values of
        <code class="literal">'12:00:00'</code>, <code class="literal">'24:00:00'</code>,
        and <code class="literal">'-12:00:00'</code>, when converted to
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> or
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> values, result in
        <code class="literal">'2012-01-01 12:00:00'</code>, <code class="literal">'2012-01-02
        00:00:00'</code>, and <code class="literal">'2011-12-31
        12:00:00'</code>, respectively.
      </p><p>
        Conversion of <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> to
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a> is similar but discards the
        time part from the result: <code class="literal">'2012-01-01'</code>,
        <code class="literal">'2012-01-02'</code>, and
        <code class="literal">'2011-12-31'</code>, respectively.
      </p><p>
        Explicit conversion can be used to override implicit conversion.
        For example, in comparison of
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a> and
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> values, the
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a> value is coerced to the
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> type by adding a time
        part of <code class="literal">'00:00:00'</code>. To perform the comparison
        by ignoring the time part of the
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> value instead, use the
        <a class="link" href="functions.html#function_cast"><code class="literal">CAST()</code></a> function in the following
        way:
      </p><pre data-lang="sql" class="programlisting"><em class="replaceable"><code>date_col</code></em> = CAST(<em class="replaceable"><code>datetime_col</code></em> AS DATE)
</pre><p>
        Conversion of <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME</code></a> and
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a> values to numeric form
        (for example, by adding <code class="literal">+0</code>) depends on
        whether the value contains a fractional seconds part.
        <a class="link" href="data-types.html#time" title="11.3.2&nbsp;The TIME Type"><code class="literal">TIME(<em class="replaceable"><code>N</code></em>)</code></a>
        or
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME(<em class="replaceable"><code>N</code></em>)</code></a>
        is converted to integer when <em class="replaceable"><code>N</code></em> is 0
        (or omitted) and to a <code class="literal">DECIMAL</code> value with
        <em class="replaceable"><code>N</code></em> decimal digits when
        <em class="replaceable"><code>N</code></em> is greater than 0:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT CURTIME(), CURTIME()+0, CURTIME(3)+0;</code></strong><font></font>
+-----------+-------------+--------------+<font></font>
| CURTIME() | CURTIME()+0 | CURTIME(3)+0 |<font></font>
+-----------+-------------+--------------+<font></font>
| 09:28:00  |       92800 |    92800.887 |<font></font>
+-----------+-------------+--------------+<font></font>
mysql&gt; <strong class="userinput"><code>SELECT NOW(), NOW()+0, NOW(3)+0;</code></strong><font></font>
+---------------------+----------------+--------------------+<font></font>
| NOW()               | NOW()+0        | NOW(3)+0           |<font></font>
+---------------------+----------------+--------------------+<font></font>
| 2012-08-15 09:28:00 | 20120815092800 | 20120815092800.889 |<font></font>
+---------------------+----------------+--------------------+<font></font>
</pre>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="two-digit-years"></a>11.3.7&nbsp;Two-Digit Years in Dates</h3>

</div>

</div>

</div>
<p>
        Date values with two-digit years are ambiguous because the
        century is unknown. Such values must be interpreted into
        four-digit form because MySQL stores years internally using four
        digits.
      </p><p>
        For <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATETIME</code></a>,
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">DATE</code></a>, and
        <a class="link" href="data-types.html#datetime" title="11.3.1&nbsp;The DATE, DATETIME, and TIMESTAMP Types"><code class="literal">TIMESTAMP</code></a> types, MySQL interprets
        dates specified with ambiguous year values using these rules:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Year values in the range <code class="literal">00-69</code> are
            converted to <code class="literal">2000-2069</code>.
          </p></li><li class="listitem"><p>
            Year values in the range <code class="literal">70-99</code> are
            converted to <code class="literal">1970-1999</code>.
</p></li></ul>
</div>
<p>
        For <code class="literal">YEAR</code>, the rules are the same, with this
        exception: A numeric <code class="literal">00</code> inserted into
        <code class="literal">YEAR(4)</code> results in <code class="literal">0000</code>
        rather than <code class="literal">2000</code>. To specify zero for
        <code class="literal">YEAR(4)</code> and have it be interpreted as
        <code class="literal">2000</code>, specify it as a string
        <code class="literal">'0'</code> or <code class="literal">'00'</code>.
      </p><p>
        Remember that these rules are only heuristics that provide
        reasonable guesses as to what your data values mean. If the
        rules used by MySQL do not produce the values you require, you
        must provide unambiguous input containing four-digit year
        values.
      </p><p>
        <code class="literal">ORDER BY</code> properly sorts
        <a class="link" href="data-types.html#year" title="11.3.3&nbsp;The YEAR Type"><code class="literal">YEAR</code></a> values that have two-digit
        years.
      </p><p>
        Some functions like <a class="link" href="functions.html#function_min"><code class="literal">MIN()</code></a> and
        <a class="link" href="functions.html#function_max"><code class="literal">MAX()</code></a> convert a
        <a class="link" href="data-types.html#year" title="11.3.3&nbsp;The YEAR Type"><code class="literal">YEAR</code></a> to a number. This means that
        a value with a two-digit year does not work properly with these
        functions. The fix in this case is to convert the
        <a class="link" href="data-types.html#year" title="11.3.3&nbsp;The YEAR Type"><code class="literal">YEAR</code></a> to four-digit year format.
</p>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="string-types"></a>11.4&nbsp;String Types</h2>

</div>

</div>

</div>
<div class="toc">
<dl class="toc"><dt><span class="section"><a href="data-types.html#char">11.4.1 The CHAR and VARCHAR Types</a></span></dt><dt><span class="section"><a href="data-types.html#binary-varbinary">11.4.2 The BINARY and VARBINARY Types</a></span></dt><dt><span class="section"><a href="data-types.html#blob">11.4.3 The BLOB and TEXT Types</a></span></dt><dt><span class="section"><a href="data-types.html#enum">11.4.4 The ENUM Type</a></span></dt><dt><span class="section"><a href="data-types.html#set">11.4.5 The SET Type</a></span></dt></dl>
</div>
<a class="indexterm" name="idm140091693176688"></a><a class="indexterm" name="idm140091693175200"></a><a class="indexterm" name="idm140091693174128"></a><a class="indexterm" name="idm140091693173056"></a><a class="indexterm" name="idm140091693171984"></a><a class="indexterm" name="idm140091693170496"></a><p>
      The string types are <a class="link" href="data-types.html#char" title="11.4.1&nbsp;The CHAR and VARCHAR Types"><code class="literal">CHAR</code></a>,
      <a class="link" href="data-types.html#char" title="11.4.1&nbsp;The CHAR and VARCHAR Types"><code class="literal">VARCHAR</code></a>,
      <a class="link" href="data-types.html#binary-varbinary" title="11.4.2&nbsp;The BINARY and VARBINARY Types"><code class="literal">BINARY</code></a>,
      <a class="link" href="data-types.html#binary-varbinary" title="11.4.2&nbsp;The BINARY and VARBINARY Types"><code class="literal">VARBINARY</code></a>,
      <a class="link" href="data-types.html#blob" title="11.4.3&nbsp;The BLOB and TEXT Types"><code class="literal">BLOB</code></a>,
      <a class="link" href="data-types.html#blob" title="11.4.3&nbsp;The BLOB and TEXT Types"><code class="literal">TEXT</code></a>,
      <a class="link" href="data-types.html#enum" title="11.4.4&nbsp;The ENUM Type"><code class="literal">ENUM</code></a>, and
      <a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET</code></a>. This section describes how
      these types work and how to use them in your queries. For string
      type storage requirements, see
      <a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求">Section&nbsp;11.8, “Data Type Storage Requirements”</a>.
</p>
<div class="section">

<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="char"></a>11.4.1&nbsp;The CHAR and VARCHAR Types</h3>
</div>
</div>
</div>
<a class="indexterm" name="idm140091693156896"></a><a class="indexterm" name="idm140091693155440"></a><a class="indexterm" name="idm140091693153952"></a><p>
        The <code class="literal">CHAR</code> and <code class="literal">VARCHAR</code> types
        are similar, but differ in the way they are stored and
        retrieved. They also differ in maximum length and in whether
        trailing spaces are retained.
      </p><p>
        The <code class="literal">CHAR</code> and <code class="literal">VARCHAR</code> types
        are declared with a length that indicates the maximum number of
        characters you want to store. For example,
        <code class="literal">CHAR(30)</code> can hold up to 30 characters.
      </p><p>
        The length of a <code class="literal">CHAR</code> column is fixed to the
        length that you declare when you create the table. The length
        can be any value from 0 to 255. When <code class="literal">CHAR</code>
        values are stored, they are right-padded with spaces to the
        specified length. When <code class="literal">CHAR</code> values are
        retrieved, trailing spaces are removed unless the
        <a class="link" href="server-administration.html#sqlmode_pad_char_to_full_length"><code class="literal">PAD_CHAR_TO_FULL_LENGTH</code></a> SQL
        mode is enabled.
      </p><p>
        Values in <code class="literal">VARCHAR</code> columns are variable-length
        strings. The length can be specified as a value from 0 to
        65,535. The effective maximum length of a
        <code class="literal">VARCHAR</code> is subject to the maximum row size
        (65,535 bytes, which is shared among all columns) and the
        character set used. See <a class="xref" href="restrictions.html#column-count-limit" title="C.10.4表列数和行大小的限制">Section&nbsp;C.10.4, “Limits on Table Column Count and Row Size”</a>.
      </p><p>
        In contrast to <code class="literal">CHAR</code>,
        <code class="literal">VARCHAR</code> values are stored as a 1-byte or
        2-byte length prefix plus data. The length prefix indicates the
        number of bytes in the value. A column uses one length byte if
        values require no more than 255 bytes, two length bytes if
        values may require more than 255 bytes.
      </p><p>
        If strict SQL mode is not enabled and you assign a value to a
        <code class="literal">CHAR</code> or <code class="literal">VARCHAR</code> column
        that exceeds the column's maximum length, the value is truncated
        to fit and a warning is generated. For truncation of nonspace
        characters, you can cause an error to occur (rather than a
        warning) and suppress insertion of the value by using strict SQL
        mode. See <a class="xref" href="server-administration.html#sql-mode" title="5.1.11服务器SQL模式">Section&nbsp;5.1.11, “Server SQL Modes”</a>.
      </p><p>
        For <code class="literal">VARCHAR</code> columns, trailing spaces in
        excess of the column length are truncated prior to insertion and
        a warning is generated, regardless of the SQL mode in use. For
        <code class="literal">CHAR</code> columns, truncation of excess trailing
        spaces from inserted values is performed silently regardless of
        the SQL mode.
      </p><p>
        <code class="literal">VARCHAR</code> values are not padded when they are
        stored. Trailing spaces are retained when values are stored and
        retrieved, in conformance with standard SQL.
      </p><p>
        The following table illustrates the differences between
        <code class="literal">CHAR</code> and <code class="literal">VARCHAR</code> by
        showing the result of storing various string values into
        <code class="literal">CHAR(4)</code> and <code class="literal">VARCHAR(4)</code>
        columns (assuming that the column uses a single-byte character
        set such as <code class="literal">latin1</code>).
</p>
<div class="informaltable">
<table summary="Illustration of the difference between CHAR and VARCHAR storage requirements by showing the required storage for various string values in CHAR(4) and VARCHAR(4) columns."><colgroup><col width="15%"><col width="15%"><col width="20%"><col width="15%"><col width="20%"></colgroup><thead><tr>
            <th scope="col">Value</th>
            <th scope="col"><code class="literal">CHAR(4)</code></th>
            <th scope="col">Storage Required</th>
            <th scope="col"><code class="literal">VARCHAR(4)</code></th>
            <th scope="col">Storage Required</th>
          </tr></thead><tbody><tr>
            <td scope="row"><code class="literal">''</code></td>
            <td><code class="literal">'&nbsp;&nbsp;&nbsp;&nbsp;'</code></td>
            <td>4 bytes</td>
            <td><code class="literal">''</code></td>
            <td>1 byte</td>
          </tr><tr>
            <td scope="row"><code class="literal">'ab'</code></td>
            <td><code class="literal">'ab&nbsp;&nbsp;'</code></td>
            <td>4 bytes</td>
            <td><code class="literal">'ab'</code></td>
            <td>3 bytes</td>
          </tr><tr>
            <td scope="row"><code class="literal">'abcd'</code></td>
            <td><code class="literal">'abcd'</code></td>
            <td>4 bytes</td>
            <td><code class="literal">'abcd'</code></td>
            <td>5 bytes</td>
          </tr><tr>
            <td scope="row"><code class="literal">'abcdefgh'</code></td>
            <td><code class="literal">'abcd'</code></td>
            <td>4 bytes</td>
            <td><code class="literal">'abcd'</code></td>
            <td>5 bytes</td>
</tr></tbody></table>
</div>
<p>
        The values shown as stored in the last row of the table apply
        <span class="emphasis"><em>only when not using strict mode</em></span>; if MySQL
        is running in strict mode, values that exceed the column length
        are <span class="emphasis"><em>not stored</em></span>, and an error results.
      </p><p>
        <code class="literal">InnoDB</code> encodes fixed-length fields greater
        than or equal to 768 bytes in length as variable-length fields,
        which can be stored off-page. For example, a
        <code class="literal">CHAR(255)</code> column can exceed 768 bytes if the
        maximum byte length of the character set is greater than 3, as
        it is with <code class="literal">utf8mb4</code>.
      </p><p>
        If a given value is stored into the <code class="literal">CHAR(4)</code>
        and <code class="literal">VARCHAR(4)</code> columns, the values retrieved
        from the columns are not always the same because trailing spaces
        are removed from <code class="literal">CHAR</code> columns upon retrieval.
        The following example illustrates this difference:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE vc (v VARCHAR(4), c CHAR(4));</code></strong><font></font>
Query OK, 0 rows affected (0.01 sec)<font></font>
<font></font>
mysql&gt; <strong class="userinput"><code>INSERT INTO vc VALUES ('ab  ', 'ab  ');</code></strong><font></font>
Query OK, 1 row affected (0.00 sec)<font></font>
<font></font>
mysql&gt; <strong class="userinput"><code>SELECT CONCAT('(', v, ')'), CONCAT('(', c, ')') FROM vc;</code></strong><font></font>
+---------------------+---------------------+<font></font>
| CONCAT('(', v, ')') | CONCAT('(', c, ')') |<font></font>
+---------------------+---------------------+<font></font>
| (ab  )              | (ab)                |<font></font>
+---------------------+---------------------+<font></font>
1 row in set (0.06 sec)<font></font>
</pre><p>
        Values in <code class="literal">CHAR</code> and <code class="literal">VARCHAR</code>
        columns are sorted and compared according to the character set
        collation assigned to the column.
      </p><a class="indexterm" name="idm140091693077408"></a><a class="indexterm" name="idm140091693075920"></a><a class="indexterm" name="idm140091693074848"></a><a class="indexterm" name="idm140091693073360"></a><p>
        Most MySQL collations have a pad attribute of PAD SPACE. The
        exceptions are Unicode collations based on UCA 9.0.0 and higher,
        which have a pad attribute of NO PAD. (see
        <a class="xref" href="charset.html#charset-unicode-sets" title="10.10.1 Unicode字符集">Section&nbsp;10.10.1, “Unicode Character Sets”</a>).
      </p><p>
        To determine the pad attribute for a collation, use the
        <code class="literal">INFORMATION_SCHEMA</code>
        <a class="link" href="information-schema.html#collations-table" title="25.4 INFORMATION_SCHEMA COLLATIONS表"><code class="literal">COLLATIONS</code></a> table, which has a
        <code class="literal">PAD_ATTRIBUTE</code> column.
      </p><p>
        The pad attribute determines how trailing spaces are treated for
        comparison of nonbinary strings (<code class="literal">CHAR</code>,
        <code class="literal">VARCHAR</code>, and <code class="literal">TEXT</code> values).
        NO PAD collations treat spaces at the end of strings like any
        other character. For PAD SPACE collations, trailing spaces are
        insignificant in comparisons; strings are compared without
        regard to any trailing spaces. <span class="quote">“<span class="quote">Comparison</span>”</span> in this
        context does not include the <a class="link" href="functions.html#operator_like"><code class="literal">LIKE</code></a>
        pattern-matching operator, for which trailing spaces are
        significant. For example:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE names (myname CHAR(10));</code></strong><font></font>
Query OK, 0 rows affected (0.03 sec)<font></font>
<font></font>
mysql&gt; <strong class="userinput"><code>INSERT INTO names VALUES ('Monty');</code></strong><font></font>
Query OK, 1 row affected (0.00 sec)<font></font>
<font></font>
mysql&gt; <strong class="userinput"><code>SELECT myname = 'Monty', myname = 'Monty  ' FROM names;</code></strong><font></font>
+------------------+--------------------+<font></font>
| myname = 'Monty' | myname = 'Monty  ' |<font></font>
+------------------+--------------------+<font></font>
|                1 |                  1 |<font></font>
+------------------+--------------------+<font></font>
1 row in set (0.00 sec)<font></font>
<font></font>
mysql&gt; <strong class="userinput"><code>SELECT myname LIKE 'Monty', myname LIKE 'Monty  ' FROM names;</code></strong><font></font>
+---------------------+-----------------------+<font></font>
| myname LIKE 'Monty' | myname LIKE 'Monty  ' |<font></font>
+---------------------+-----------------------+<font></font>
|                   1 |                     0 |<font></font>
+---------------------+-----------------------+<font></font>
1 row in set (0.00 sec)<font></font>
</pre><p>
        This is true for all MySQL versions, and is not affected by the
        server SQL mode.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title">
Note
</div>
<p>
          For more information about MySQL character sets and
          collations, see <a class="xref" href="charset.html" title="第10章字符集，排序规则，Unicode">Chapter&nbsp;10, <i>Character Sets, Collations, Unicode</i></a>. For additional
          information about storage requirements, see
          <a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求">Section&nbsp;11.8, “Data Type Storage Requirements”</a>.
</p>
</div>
<p>
        For those cases where trailing pad characters are stripped or
        comparisons ignore them, if a column has an index that requires
        unique values, inserting into the column values that differ only
        in number of trailing pad characters will result in a
        duplicate-key error. For example, if a table contains
        <code class="literal">'a'</code>, an attempt to store
        <code class="literal">'a&nbsp;'</code> causes a duplicate-key error.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="binary-varbinary"></a>11.4.2&nbsp;The BINARY and VARBINARY Types</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091693052080"></a><a class="indexterm" name="idm140091693051040"></a><a class="indexterm" name="idm140091693049968"></a><a class="indexterm" name="idm140091693048480"></a><p>
        The <code class="literal">BINARY</code> and <code class="literal">VARBINARY</code>
        types are similar to <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a> and
        <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a>, except that they contain
        binary strings rather than nonbinary strings. That is, they
        contain byte strings rather than character strings. This means
        they have the <code class="literal">binary</code> character set and
        collation, and comparison and sorting are based on the numeric
        values of the bytes in the values.
      </p><p>
        The permissible maximum length is the same for
        <code class="literal">BINARY</code> and <code class="literal">VARBINARY</code> as it
        is for <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a> and
        <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a>, except that the length
        for <code class="literal">BINARY</code> and <code class="literal">VARBINARY</code>
        is a length in bytes rather than in characters.
      </p><p>
        The <code class="literal">BINARY</code> and <code class="literal">VARBINARY</code>
        data types are distinct from the <code class="literal">CHAR BINARY</code>
        and <code class="literal">VARCHAR BINARY</code> data types. For the latter
        types, the <code class="literal">BINARY</code> attribute does not cause
        the column to be treated as a binary string column. Instead, it
        causes the binary (<code class="literal">_bin</code>) collation for the
        column character set to be used, and the column itself contains
        nonbinary character strings rather than binary byte strings. For
        example, <code class="literal">CHAR(5) BINARY</code> is treated as
        <code class="literal">CHAR(5) CHARACTER SET utf8mb4 COLLATE
        utf8mb4_bin</code>, assuming that the default character set
        is <code class="literal">utf8mb4</code>. This differs from
        <code class="literal">BINARY(5)</code>, which stores 5-bytes binary
        strings that have the <code class="literal">binary</code> character set
        and collation. For information about differences between binary
        strings and binary collations for nonbinary strings, see
        <a class="xref" href="charset.html#charset-binary-collations" title="10.8.5二进制排序与_bin排序相比">Section&nbsp;10.8.5, “The binary Collation Compared to _bin Collations”</a>.
      </p><p>
        If strict SQL mode is not enabled and you assign a value to a
        <code class="literal">BINARY</code> or <code class="literal">VARBINARY</code> column
        that exceeds the column's maximum length, the value is truncated
        to fit and a warning is generated. For cases of truncation, you
        can cause an error to occur (rather than a warning) and suppress
        insertion of the value by using strict SQL mode. See
        <a class="xref" href="server-administration.html#sql-mode" title="5.1.11服务器SQL模式">Section&nbsp;5.1.11, “Server SQL Modes”</a>.
      </p><p>
        When <code class="literal">BINARY</code> values are stored, they are
        right-padded with the pad value to the specified length. The pad
        value is <code class="literal">0x00</code> (the zero byte). Values are
        right-padded with <code class="literal">0x00</code> on insert, and no
        trailing bytes are removed on select. All bytes are significant
        in comparisons, including <code class="literal">ORDER BY</code> and
        <code class="literal">DISTINCT</code> operations. <code class="literal">0x00</code>
        bytes and spaces are different in comparisons, with
        <code class="literal">0x00</code> &lt; space.
      </p><p>
        Example: For a <code class="literal">BINARY(3)</code> column,
        <code class="literal">'a&nbsp;'</code> becomes
        <code class="literal">'a&nbsp;\0'</code> when inserted.
        <code class="literal">'a\0'</code> becomes <code class="literal">'a\0\0'</code> when
        inserted. Both inserted values remain unchanged when selected.
      </p><p>
        For <code class="literal">VARBINARY</code>, there is no padding on insert
        and no bytes are stripped on select. All bytes are significant
        in comparisons, including <code class="literal">ORDER BY</code> and
        <code class="literal">DISTINCT</code> operations. <code class="literal">0x00</code>
        bytes and spaces are different in comparisons, with
        <code class="literal">0x00</code> &lt; space.
      </p><p>
        For those cases where trailing pad bytes are stripped or
        comparisons ignore them, if a column has an index that requires
        unique values, inserting into the column values that differ only
        in number of trailing pad bytes will result in a duplicate-key
        error. For example, if a table contains <code class="literal">'a'</code>,
        an attempt to store <code class="literal">'a\0'</code> causes a
        duplicate-key error.
      </p><p>
        You should consider the preceding padding and stripping
        characteristics carefully if you plan to use the
        <code class="literal">BINARY</code> data type for storing binary data and
        you require that the value retrieved be exactly the same as the
        value stored. The following example illustrates how
        <code class="literal">0x00</code>-padding of <code class="literal">BINARY</code>
        values affects column value comparisons:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE t (c BINARY(3));</code></strong><font></font>
Query OK, 0 rows affected (0.01 sec)<font></font>
<font></font>
mysql&gt; <strong class="userinput"><code>INSERT INTO t SET c = 'a';</code></strong><font></font>
Query OK, 1 row affected (0.01 sec)<font></font>
<font></font>
mysql&gt; <strong class="userinput"><code>SELECT HEX(c), c = 'a', c = 'a\0\0' from t;</code></strong><font></font>
+--------+---------+-------------+<font></font>
| HEX(c) | c = 'a' | c = 'a\0\0' |<font></font>
+--------+---------+-------------+<font></font>
| 610000 |       0 |           1 |<font></font>
+--------+---------+-------------+<font></font>
1 row in set (0.09 sec)<font></font>
</pre><p>
        If the value retrieved must be the same as the value specified
        for storage with no padding, it might be preferable to use
        <code class="literal">VARBINARY</code> or one of the
        <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a> data types instead.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="blob"></a>11.4.3&nbsp;The BLOB and TEXT Types</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091692995488"></a><a class="indexterm" name="idm140091692994416"></a><a class="indexterm" name="idm140091692993344"></a><a class="indexterm" name="idm140091692991856"></a><a class="indexterm" name="idm140091692990368"></a><a class="indexterm" name="idm140091692989296"></a><p>
        A <code class="literal">BLOB</code> is a binary large object that can hold
        a variable amount of data. The four <code class="literal">BLOB</code>
        types are <code class="literal">TINYBLOB</code>, <code class="literal">BLOB</code>,
        <code class="literal">MEDIUMBLOB</code>, and <code class="literal">LONGBLOB</code>.
        These differ only in the maximum length of the values they can
        hold. The four <code class="literal">TEXT</code> types are
        <code class="literal">TINYTEXT</code>, <code class="literal">TEXT</code>,
        <code class="literal">MEDIUMTEXT</code>, and <code class="literal">LONGTEXT</code>.
        These correspond to the four <code class="literal">BLOB</code> types and
        have the same maximum lengths and storage requirements. See
        <a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求">Section&nbsp;11.8, “Data Type Storage Requirements”</a>.
      </p><p>
        <code class="literal">BLOB</code> values are treated as binary strings
        (byte strings). They have the <code class="literal">binary</code>
        character set and collation, and comparison and sorting are
        based on the numeric values of the bytes in column values.
        <code class="literal">TEXT</code> values are treated as nonbinary strings
        (character strings). They have a character set other than
        <code class="literal">binary</code>, and values are sorted and compared
        based on the collation of the character set.
      </p><p>
        If strict SQL mode is not enabled and you assign a value to a
        <code class="literal">BLOB</code> or <code class="literal">TEXT</code> column that
        exceeds the column's maximum length, the value is truncated to
        fit and a warning is generated. For truncation of nonspace
        characters, you can cause an error to occur (rather than a
        warning) and suppress insertion of the value by using strict SQL
        mode. See <a class="xref" href="server-administration.html#sql-mode" title="5.1.11服务器SQL模式">Section&nbsp;5.1.11, “Server SQL Modes”</a>.
      </p><p>
        Truncation of excess trailing spaces from values to be inserted
        into <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a> columns always
        generates a warning, regardless of the SQL mode.
      </p><p>
        For <code class="literal">TEXT</code> and <code class="literal">BLOB</code> columns,
        there is no padding on insert and no bytes are stripped on
        select.
      </p><p>
        If a <code class="literal">TEXT</code> column is indexed, index entry
        comparisons are space-padded at the end. This means that, if the
        index requires unique values, duplicate-key errors will occur
        for values that differ only in the number of trailing spaces.
        For example, if a table contains <code class="literal">'a'</code>, an
        attempt to store <code class="literal">'a&nbsp;'</code> causes a
        duplicate-key error. This is not true for
        <code class="literal">BLOB</code> columns.
      </p><p>
        In most respects, you can regard a <code class="literal">BLOB</code>
        column as a <a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">VARBINARY</code></a> column that
        can be as large as you like. Similarly, you can regard a
        <code class="literal">TEXT</code> column as a
        <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a> column.
        <code class="literal">BLOB</code> and <code class="literal">TEXT</code> differ from
        <a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">VARBINARY</code></a> and
        <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a> in the following ways:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            For indexes on <code class="literal">BLOB</code> and
            <code class="literal">TEXT</code> columns, you must specify an index
            prefix length. For <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a> and
            <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a>, a prefix length is
            optional. See <a class="xref" href="optimization.html#column-indexes" title="8.3.5列索引">Section&nbsp;8.3.5, “Column Indexes”</a>.
          </p></li><li class="listitem"><p>
            <a class="indexterm" name="idm140091692948992"></a>

            <a class="indexterm" name="idm140091692947504"></a>

            <a class="indexterm" name="idm140091692946016"></a>

            <code class="literal">BLOB</code> and <code class="literal">TEXT</code> columns
            cannot have <code class="literal">DEFAULT</code> values.
</p></li></ul>
</div>
<p>
        If you use the <code class="literal">BINARY</code> attribute with a
        <code class="literal">TEXT</code> data type, the column is assigned the
        binary (<code class="literal">_bin</code>) collation of the column
        character set.
      </p><p>
        <code class="literal">LONG</code> and <code class="literal">LONG VARCHAR</code> map
        to the <code class="literal">MEDIUMTEXT</code> data type. This is a
        compatibility feature.
      </p><p>
        MySQL Connector/ODBC defines <code class="literal">BLOB</code> values as
        <code class="literal">LONGVARBINARY</code> and <code class="literal">TEXT</code>
        values as <code class="literal">LONGVARCHAR</code>.
      </p><p>
        Because <code class="literal">BLOB</code> and <code class="literal">TEXT</code>
        values can be extremely long, you might encounter some
        constraints in using them:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Only the first
            <a class="link" href="server-administration.html#sysvar_max_sort_length"><code class="literal">max_sort_length</code></a> bytes of
            the column are used when sorting. The default value of
            <a class="link" href="server-administration.html#sysvar_max_sort_length"><code class="literal">max_sort_length</code></a> is 1024.
            You can make more bytes significant in sorting or grouping
            by increasing the value of
            <a class="link" href="server-administration.html#sysvar_max_sort_length"><code class="literal">max_sort_length</code></a> at server
            startup or runtime. Any client can change the value of its
            session <a class="link" href="server-administration.html#sysvar_max_sort_length"><code class="literal">max_sort_length</code></a>
            variable:
          </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SET max_sort_length = 2000;</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT id, comment FROM t</code></strong>
    -&gt; <strong class="userinput"><code>ORDER BY comment;</code></strong>
</pre></li><li class="listitem"><p>
            Instances of <code class="literal">BLOB</code> or
            <code class="literal">TEXT</code> columns in the result of a query
            that is processed using a temporary table causes the server
            to use a table on disk rather than in memory because the
            <code class="literal">MEMORY</code> storage engine does not support
            those data types (see
            <a class="xref" href="optimization.html#internal-temporary-tables" title="8.4.4 MySQL中的内部临时表使用">Section&nbsp;8.4.4, “Internal Temporary Table Use in MySQL”</a>). Use of disk
            incurs a performance penalty, so include
            <code class="literal">BLOB</code> or <code class="literal">TEXT</code> columns
            in the query result only if they are really needed. For
            example, avoid using
            <a class="link" href="sql-syntax.html#select" title="13.2.10 SELECT语法"><code class="literal">SELECT *</code></a>,
            which selects all columns.
          </p></li><li class="listitem"><p>
            The maximum size of a <code class="literal">BLOB</code> or
            <code class="literal">TEXT</code> object is determined by its type,
            but the largest value you actually can transmit between the
            client and server is determined by the amount of available
            memory and the size of the communications buffers. You can
            change the message buffer size by changing the value of the
            <a class="link" href="server-administration.html#sysvar_max_allowed_packet"><code class="literal">max_allowed_packet</code></a>
            variable, but you must do so for both the server and your
            client program. For example, both <a class="link" href="programs.html#mysql" title="4.5.1 mysql  -  MySQL命令行客户端"><span class="command"><strong>mysql</strong></span></a>
            and <a class="link" href="programs.html#mysqldump" title="4.5.4 mysqldump  - 数据库备份程序"><span class="command"><strong>mysqldump</strong></span></a> enable you to change the
            client-side
            <a class="link" href="server-administration.html#sysvar_max_allowed_packet"><code class="literal">max_allowed_packet</code></a> value.
            See <a class="xref" href="server-administration.html#server-configuration" title="5.1.1配置服务器">Section&nbsp;5.1.1, “Configuring the Server”</a>,
            <a class="xref" href="programs.html#mysql" title="4.5.1 mysql  -  MySQL命令行客户端">Section&nbsp;4.5.1, “<span class="command"><strong>mysql</strong></span> — The MySQL Command-Line Client”</a>, and <a class="xref" href="programs.html#mysqldump" title="4.5.4 mysqldump  - 数据库备份程序">Section&nbsp;4.5.4, “<span class="command"><strong>mysqldump</strong></span> — A Database Backup Program”</a>.
            You may also want to compare the packet sizes and the size
            of the data objects you are storing with the storage
            requirements, see <a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求">Section&nbsp;11.8, “Data Type Storage Requirements”</a>
</p></li></ul>
</div>
<p>
        Each <code class="literal">BLOB</code> or <code class="literal">TEXT</code> value is
        represented internally by a separately allocated object. This is
        in contrast to all other data types, for which storage is
        allocated once per column when the table is opened.
      </p><p>
        In some cases, it may be desirable to store binary data such as
        media files in <code class="literal">BLOB</code> or
        <code class="literal">TEXT</code> columns. You may find MySQL's string
        handling functions useful for working with such data. See
        <a class="xref" href="functions.html#string-functions" title="12.5字符串函数">Section&nbsp;12.5, “String Functions”</a>. For security and other
        reasons, it is usually preferable to do so using application
        code rather than giving application users the
        <a class="link" href="security.html#priv_file"><code class="literal">FILE</code></a> privilege. You can discuss
        specifics for various languages and platforms in the MySQL
        Forums (<a class="ulink" href="http://forums.mysql.com/" target="_top">http://forums.mysql.com/</a>).
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="enum"></a>11.4.4&nbsp;The ENUM Type</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091692896544"></a><a class="indexterm" name="idm140091692895472"></a><p>
        An <code class="literal">ENUM</code> is a string object with a value
        chosen from a list of permitted values that are enumerated
        explicitly in the column specification at table creation time.
        It has these advantages:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Compact data storage in situations where a column has a
            limited set of possible values. The strings you specify as
            input values are automatically encoded as numbers. See
            <a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求">Section&nbsp;11.8, “Data Type Storage Requirements”</a> for the storage
            requirements for <code class="literal">ENUM</code> types.
          </p></li><li class="listitem"><p>
            Readable queries and output. The numbers are translated back
            to the corresponding strings in query results.
</p></li></ul>
</div>
<p>
        and these potential issues to consider:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            If you make enumeration values that look like numbers, it is
            easy to mix up the literal values with their internal index
            numbers, as explained in <a class="xref" href="data-types.html#enum-limits" title="枚举限制">Enumeration Limitations</a>.
          </p></li><li class="listitem"><p>
            Using <code class="literal">ENUM</code> columns in <code class="literal">ORDER
            BY</code> clauses requires extra care, as explained in
            <a class="xref" href="data-types.html#enum-sorting" title="枚举排序">Enumeration Sorting</a>.
</p></li></ul>
</div>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><a class="xref" href="data-types.html#enum-using" title="创建和使用ENUM列">Creating and Using ENUM Columns</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#enum-indexes" title="枚举文字的索引值">Index Values for Enumeration Literals</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#enum-literals" title="处理枚举文字">Handling of Enumeration Literals</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#enum-nulls" title="空或NULL枚举值">Empty or NULL Enumeration Values</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#enum-sorting" title="枚举排序">Enumeration Sorting</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#enum-limits" title="枚举限制">Enumeration Limitations</a></p></li></ul>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h4 class="title"><a name="enum-using"></a>Creating and Using ENUM Columns</h4>

</div>

</div>

</div>
<p>
          An enumeration value must be a quoted string literal. For
          example, you can create a table with an
          <code class="literal">ENUM</code> column like this:
        </p><pre data-lang="sql" class="programlisting">CREATE TABLE shirts (<font></font>
    name VARCHAR(40),<font></font>
    size ENUM('x-small', 'small', 'medium', 'large', 'x-large')<font></font>
);<font></font>
INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),<font></font>
  ('polo shirt','small');<font></font>
SELECT name, size FROM shirts WHERE size = 'medium';<font></font>
+---------+--------+<font></font>
| name    | size   |<font></font>
+---------+--------+<font></font>
| t-shirt | medium |<font></font>
+---------+--------+<font></font>
UPDATE shirts SET size = 'small' WHERE size = 'large';<font></font>
COMMIT;<font></font>
</pre><p>
          Inserting 1 million rows into this table with a value of
          <code class="literal">'medium'</code> would require 1 million bytes of
          storage, as opposed to 6 million bytes if you stored the
          actual string <code class="literal">'medium'</code> in a
          <code class="literal">VARCHAR</code> column.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h4 class="title"><a name="enum-indexes"></a>Index Values for Enumeration Literals</h4>

</div>

</div>

</div>
<p>
          Each enumeration value has an index:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              The elements listed in the column specification are
              assigned index numbers, beginning with 1.
            </p></li><li class="listitem"><p>
              The index value of the empty string error value is 0. This
              means that you can use the following
              <a class="link" href="sql-syntax.html#select" title="13.2.10 SELECT语法"><code class="literal">SELECT</code></a> statement to find
              rows into which invalid <code class="literal">ENUM</code> values
              were assigned:
            </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT * FROM <em class="replaceable"><code>tbl_name</code></em> WHERE <em class="replaceable"><code>enum_col</code></em>=0;</code></strong>
</pre></li><li class="listitem"><p>
              The index of the <code class="literal">NULL</code> value is
              <code class="literal">NULL</code>.
            </p></li><li class="listitem"><p>
              The term <span class="quote">“<span class="quote">index</span>”</span> here refers to a position
              within the list of enumeration values. It has nothing to
              do with table indexes.
</p></li></ul>
</div>
<p>
          For example, a column specified as <code class="literal">ENUM('Mercury',
          'Venus', 'Earth')</code> can have any of the values shown
          here. The index of each value is also shown.
</p>
<div class="informaltable">
<table summary="Possible values for a column specified as ENUM('Mercury', 'Venus', 'Earth'). The table also shows the index of each value."><colgroup><col width="15%"><col width="15%"></colgroup><thead><tr>
              <th scope="col">Value</th>
              <th scope="col">Index</th>
            </tr></thead><tbody><tr>
              <td scope="row"><code class="literal">NULL</code></td>
              <td><code class="literal">NULL</code></td>
            </tr><tr>
              <td scope="row"><code class="literal">''</code></td>
              <td>0</td>
            </tr><tr>
              <td scope="row"><code class="literal">'Mercury'</code></td>
              <td>1</td>
            </tr><tr>
              <td scope="row"><code class="literal">'Venus'</code></td>
              <td>2</td>
            </tr><tr>
              <td scope="row"><code class="literal">'Earth'</code></td>
              <td>3</td>
</tr></tbody></table>
</div>
<p>
          An <a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM</code></a> column can have a
          maximum of 65,535 distinct elements.
        </p><p>
          If you retrieve an <code class="literal">ENUM</code> value in a numeric
          context, the column value's index is returned. For example,
          you can retrieve numeric values from an
          <code class="literal">ENUM</code> column like this:
        </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT <em class="replaceable"><code>enum_col</code></em>+0 FROM <em class="replaceable"><code>tbl_name</code></em>;</code></strong>
</pre><p>
          Functions such as <a class="link" href="functions.html#function_sum"><code class="literal">SUM()</code></a> or
          <a class="link" href="functions.html#function_avg"><code class="literal">AVG()</code></a> that expect a numeric
          argument cast the argument to a number if necessary. For
          <code class="literal">ENUM</code> values, the index number is used in
          the calculation.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h4 class="title"><a name="enum-literals"></a>Handling of Enumeration Literals</h4>

</div>

</div>

</div>
<a class="indexterm" name="idm140091692825632"></a><p>
          Trailing spaces are automatically deleted from
          <code class="literal">ENUM</code> member values in the table definition
          when a table is created.
        </p><p>
          When retrieved, values stored into an <code class="literal">ENUM</code>
          column are displayed using the lettercase that was used in the
          column definition. Note that <code class="literal">ENUM</code> columns
          can be assigned a character set and collation. For binary or
          case-sensitive collations, lettercase is taken into account
          when assigning values to the column.
        </p><p>
          If you store a number into an <code class="literal">ENUM</code> column,
          the number is treated as the index into the possible values,
          and the value stored is the enumeration member with that
          index. (However, this does <span class="emphasis"><em>not</em></span> work with
          <a class="link" href="sql-syntax.html#load-data" title="13.2.7 LOAD DATA语法"><code class="literal">LOAD DATA</code></a>, which treats all
          input as strings.) If the numeric value is quoted, it is still
          interpreted as an index if there is no matching string in the
          list of enumeration values. For these reasons, it is not
          advisable to define an <code class="literal">ENUM</code> column with
          enumeration values that look like numbers, because this can
          easily become confusing. For example, the following column has
          enumeration members with string values of
          <code class="literal">'0'</code>, <code class="literal">'1'</code>, and
          <code class="literal">'2'</code>, but numeric index values of
          <code class="literal">1</code>, <code class="literal">2</code>, and
          <code class="literal">3</code>:
        </p><pre data-lang="sql" class="programlisting">numbers ENUM('0','1','2')
</pre><p>
          If you store <code class="literal">2</code>, it is interpreted as an
          index value, and becomes <code class="literal">'1'</code> (the value
          with index 2). If you store <code class="literal">'2'</code>, it matches
          an enumeration value, so it is stored as
          <code class="literal">'2'</code>. If you store <code class="literal">'3'</code>,
          it does not match any enumeration value, so it is treated as
          an index and becomes <code class="literal">'2'</code> (the value with
          index 3).
        </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>INSERT INTO t (numbers) VALUES(2),('2'),('3');</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT * FROM t;</code></strong><font></font>
+---------+<font></font>
| numbers |<font></font>
+---------+<font></font>
| 1       |<font></font>
| 2       |<font></font>
| 2       |<font></font>
+---------+<font></font>
</pre><p>
          To determine all possible values for an
          <code class="literal">ENUM</code> column, use
          <a class="link" href="sql-syntax.html#show-columns" title="13.7.6.5 SHOW COLUMNS语法"><code class="literal">SHOW COLUMNS
          FROM <em class="replaceable"><code>tbl_name</code></em> LIKE
          '<em class="replaceable"><code>enum_col</code></em>'</code></a> and parse the
          <code class="literal">ENUM</code> definition in the
          <code class="literal">Type</code> column of the output.
        </p><p>
          In the C API, <code class="literal">ENUM</code> values are returned as
          strings. For information about using result set metadata to
          distinguish them from other strings, see
          <a class="xref" href="connectors-apis.html#c-api-data-structures" title="28.7.5 C API数据结构">Section&nbsp;28.7.5, “C API Data Structures”</a>.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h4 class="title"><a name="enum-nulls"></a>Empty or NULL Enumeration Values</h4>

</div>

</div>

</div>
<p>
          An enumeration value can also be the empty string
          (<code class="literal">''</code>) or <code class="literal">NULL</code> under
          certain circumstances:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              If you insert an invalid value into an
              <code class="literal">ENUM</code> (that is, a string not present in
              the list of permitted values), the empty string is
              inserted instead as a special error value. This string can
              be distinguished from a <span class="quote">“<span class="quote">normal</span>”</span> empty string
              by the fact that this string has the numeric value 0. See
              <a class="xref" href="data-types.html#enum-indexes" title="枚举文字的索引值">Index Values for Enumeration Literals</a> for details about the
              numeric indexes for the enumeration values.
            </p><p>
              If strict SQL mode is enabled, attempts to insert invalid
              <code class="literal">ENUM</code> values result in an error.
            </p></li><li class="listitem"><p>
              If an <code class="literal">ENUM</code> column is declared to permit
              <code class="literal">NULL</code>, the <code class="literal">NULL</code> value
              is a valid value for the column, and the default value is
              <code class="literal">NULL</code>. If an <code class="literal">ENUM</code>
              column is declared <code class="literal">NOT NULL</code>, its
              default value is the first element of the list of
              permitted values.
</p></li></ul>
</div>

</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h4 class="title"><a name="enum-sorting"></a>Enumeration Sorting</h4>

</div>

</div>

</div>
<p>
          <code class="literal">ENUM</code> values are sorted based on their index
          numbers, which depend on the order in which the enumeration
          members were listed in the column specification. For example,
          <code class="literal">'b'</code> sorts before <code class="literal">'a'</code> for
          <code class="literal">ENUM('b', 'a')</code>. The empty string sorts
          before nonempty strings, and <code class="literal">NULL</code> values
          sort before all other enumeration values.
        </p><p>
          To prevent unexpected results when using the <code class="literal">ORDER
          BY</code> clause on an <code class="literal">ENUM</code> column, use
          one of these techniques:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              Specify the <code class="literal">ENUM</code> list in alphabetic
              order.
            </p></li><li class="listitem"><p>
              Make sure that the column is sorted lexically rather than
              by index number by coding <code class="literal">ORDER BY
              CAST(<em class="replaceable"><code>col</code></em> AS CHAR)</code> or
              <code class="literal">ORDER BY
              CONCAT(<em class="replaceable"><code>col</code></em>)</code>.
</p></li></ul>
</div>

</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h4 class="title"><a name="enum-limits"></a>Enumeration Limitations</h4>

</div>

</div>

</div>
<p>
          An enumeration value cannot be an expression, even one that
          evaluates to a string value.
        </p><p>
          For example, this <a class="link" href="sql-syntax.html#create-table" title="13.1.20 CREATE TABLE语法"><code class="literal">CREATE TABLE</code></a>
          statement does <span class="emphasis"><em>not</em></span> work because the
          <code class="literal">CONCAT</code> function cannot be used to construct
          an enumeration value:
        </p><pre data-lang="sql" class="programlisting">CREATE TABLE sizes (<font></font>
    size ENUM('small', CONCAT('med','ium'), 'large')<font></font>
);<font></font>
</pre><p>
          You also cannot employ a user variable as an enumeration
          value. This pair of statements do <span class="emphasis"><em>not</em></span>
          work:
        </p><pre data-lang="sql" class="programlisting">SET @mysize = 'medium';<font></font>
<font></font>
CREATE TABLE sizes (<font></font>
    size ENUM('small', @mysize, 'large')<font></font>
);<font></font>
</pre><p>
          We strongly recommend that you do <span class="emphasis"><em>not</em></span> use
          numbers as enumeration values, because it does not save on
          storage over the appropriate
          <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">TINYINT</code></a> or
          <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">SMALLINT</code></a> type, and it is easy
          to mix up the strings and the underlying number values (which
          might not be the same) if you quote the
          <code class="literal">ENUM</code> values incorrectly. If you do use a
          number as an enumeration value, always enclose it in quotation
          marks. If the quotation marks are omitted, the number is
          regarded as an index. See <a class="xref" href="data-types.html#enum-literals" title="处理枚举文字">Handling of Enumeration Literals</a> to
          see how even a quoted number could be mistakenly used as a
          numeric index value.
        </p><p>
          Duplicate values in the definition cause a warning, or an
          error if strict SQL mode is enabled.
</p>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="set"></a>11.4.5&nbsp;The SET Type</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091692756896"></a><a class="indexterm" name="idm140091692755824"></a><p>
        A <code class="literal">SET</code> is a string object that can have zero
        or more values, each of which must be chosen from a list of
        permitted values specified when the table is created.
        <code class="literal">SET</code> column values that consist of multiple
        set members are specified with members separated by commas
        (<code class="literal">,</code>). A consequence of this is that
        <code class="literal">SET</code> member values should not themselves
        contain commas.
      </p><p>
        For example, a column specified as <code class="literal">SET('one', 'two')
        NOT NULL</code> can have any of these values:
      </p><pre data-lang="simple" class="programlisting">''<font></font>
'one'<font></font>
'two'<font></font>
'one,two'<font></font>
</pre><p>
        A <a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET</code></a> column can have a maximum
        of 64 distinct members.
      </p><p>
        Duplicate values in the definition cause a warning, or an error
        if strict SQL mode is enabled.
      </p><a class="indexterm" name="idm140091692746544"></a><p>
        Trailing spaces are automatically deleted from
        <code class="literal">SET</code> member values in the table definition
        when a table is created.
      </p><p>
        When retrieved, values stored in a <code class="literal">SET</code> column
        are displayed using the lettercase that was used in the column
        definition. Note that <code class="literal">SET</code> columns can be
        assigned a character set and collation. For binary or
        case-sensitive collations, lettercase is taken into account when
        assigning values to the column.
      </p><p>
        MySQL stores <code class="literal">SET</code> values numerically, with the
        low-order bit of the stored value corresponding to the first set
        member. If you retrieve a <code class="literal">SET</code> value in a
        numeric context, the value retrieved has bits set corresponding
        to the set members that make up the column value. For example,
        you can retrieve numeric values from a <code class="literal">SET</code>
        column like this:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT <em class="replaceable"><code>set_col</code></em>+0 FROM <em class="replaceable"><code>tbl_name</code></em>;</code></strong>
</pre><p>
        If a number is stored into a <code class="literal">SET</code> column, the
        bits that are set in the binary representation of the number
        determine the set members in the column value. For a column
        specified as <code class="literal">SET('a','b','c','d')</code>, the
        members have the following decimal and binary values.
</p>
<div class="informaltable">
<table summary="Decimal and binary values for members of a column specified as SET('a','b','c','d')."><colgroup><col width="15%"><col width="20%"><col width="20%"></colgroup><thead><tr>
            <th scope="col"><code class="literal">SET</code> Member</th>
            <th scope="col">Decimal Value</th>
            <th scope="col">Binary Value</th>
          </tr></thead><tbody><tr>
            <td scope="row"><code class="literal">'a'</code></td>
            <td><code class="literal">1</code></td>
            <td><code class="literal">0001</code></td>
          </tr><tr>
            <td scope="row"><code class="literal">'b'</code></td>
            <td><code class="literal">2</code></td>
            <td><code class="literal">0010</code></td>
          </tr><tr>
            <td scope="row"><code class="literal">'c'</code></td>
            <td><code class="literal">4</code></td>
            <td><code class="literal">0100</code></td>
          </tr><tr>
            <td scope="row"><code class="literal">'d'</code></td>
            <td><code class="literal">8</code></td>
            <td><code class="literal">1000</code></td>
</tr></tbody></table>
</div>
<p>
        If you assign a value of <code class="literal">9</code> to this column,
        that is <code class="literal">1001</code> in binary, so the first and
        fourth <code class="literal">SET</code> value members
        <code class="literal">'a'</code> and <code class="literal">'d'</code> are selected
        and the resulting value is <code class="literal">'a,d'</code>.
      </p><p>
        For a value containing more than one <code class="literal">SET</code>
        element, it does not matter what order the elements are listed
        in when you insert the value. It also does not matter how many
        times a given element is listed in the value. When the value is
        retrieved later, each element in the value appears once, with
        elements listed according to the order in which they were
        specified at table creation time. For example, suppose that a
        column is specified as <code class="literal">SET('a','b','c','d')</code>:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));</code></strong>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如果插入值</font></font><code class="literal">'a,d'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><code class="literal">'d,a'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，</font></font><code class="literal">'a,d,d'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><code class="literal">'a,d,a'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，和</font></font><code class="literal">'d,a,d'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>INSERT INTO myset (col) VALUES </code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
- &gt;（'a，d'），（'d，a'），（'a，d，a'），（'a，d，d'），（'d，a，d'）;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
查询OK，5行受影响（0.01秒）</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
记录：5个重复：0个警告：0</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        然后所有这些值都显示为</font></font><code class="literal">'a,d'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">检索时：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT col FROM myset;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">col |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
5行（0.04秒）</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如果将</font></font><code class="literal">SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列设置为不受支持的值，则会忽略该值并发出警告：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>INSERT INTO myset (col) VALUES ('a,d,d,s');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
查询正常，1行受影响，1警告（0.03秒）</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SHOW WARNINGS;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ --------- + ------ + -------------------------------- ---------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">等级| </font><font style="vertical-align: inherit;">代码| </font><font style="vertical-align: inherit;">消息|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ --------- + ------ + -------------------------------- ---------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">警告| </font><font style="vertical-align: inherit;">1265 | </font><font style="vertical-align: inherit;">第1行|列'col'的数据被截断</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ --------- + ------ + -------------------------------- ---------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
1排（0.04秒）</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT col FROM myset;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">col |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a，d |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
6行（0.01秒）</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如果启用了严格的SQL模式，则尝试插入无效
         </font></font><code class="literal">SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值会导致错误。
      </font></font></p><p>
        <code class="literal">SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值按数字排序。
        </font></font><code class="literal">NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值在非</font></font><code class="literal">NULL</code> <code class="literal">SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值</font><font style="vertical-align: inherit;">之前排序</font><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">如果需要，</font><font style="vertical-align: inherit;">
        诸如</font></font><a class="link" href="functions.html#function_sum"><code class="literal">SUM()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
         </font></font><a class="link" href="functions.html#function_avg"><code class="literal">AVG()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">期望数字参数的函数将参数强制转换为数字。</font><font style="vertical-align: inherit;">对于
         </font></font><code class="literal">SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值，强制转换操作会导致使用数值。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        通常，您</font></font><code class="literal">SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">使用</font></font><a class="link" href="functions.html#function_find-in-set"><code class="literal">FIND_IN_SET()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">函数或
         </font></font><a class="link" href="functions.html#operator_like"><code class="literal">LIKE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">运算符</font><font style="vertical-align: inherit;">搜索</font><font style="vertical-align: inherit;">值
         </font><font style="vertical-align: inherit;">：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql&gt; </font><font style="vertical-align: inherit;">
mysql&gt;</font></font><strong class="userinput"><code>SELECT * FROM <em class="replaceable"><code>tbl_name</code></em> WHERE FIND_IN_SET('<em class="replaceable"><code>value</code></em>',<em class="replaceable"><code>set_col</code></em>)&gt;0;</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>SELECT * FROM <em class="replaceable"><code>tbl_name</code></em> WHERE <em class="replaceable"><code>set_col</code></em> LIKE '%<em class="replaceable"><code>value</code></em>%';</code></strong>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        第一个语句查找</font></font><em class="replaceable"><code>set_col</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">包含
         </font></font><em class="replaceable"><code>value</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">set成员的</font><font style="vertical-align: inherit;">行
         </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">第二个是类似的，但不一样：它找到</font></font><em class="replaceable"><code>set_col</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">包含
         </font></font><em class="replaceable"><code>value</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">任何地方的</font><font style="vertical-align: inherit;">行
         </font><font style="vertical-align: inherit;">，即使是另一个set成员的子串。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        以下陈述也是允许的：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql&gt; </font><font style="vertical-align: inherit;">
mysql&gt;</font></font><strong class="userinput"><code>SELECT * FROM <em class="replaceable"><code>tbl_name</code></em> WHERE <em class="replaceable"><code>set_col</code></em> &amp; 1;</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>SELECT * FROM <em class="replaceable"><code>tbl_name</code></em> WHERE <em class="replaceable"><code>set_col</code></em> = '<em class="replaceable"><code>val1</code></em>,<em class="replaceable"><code>val2</code></em>';</code></strong>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        这些语句中的第一个查找包含第一个set成员的值。</font><font style="vertical-align: inherit;">第二个寻找完全匹配。</font><font style="vertical-align: inherit;">小心比较第二种类型。</font><font style="vertical-align: inherit;">比较设定值以
         </font><font style="vertical-align: inherit;">
        返回与比较值不同的结果
         </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">您应该按照列定义中列出的顺序指定值。
      </font></font><code class="literal">'<em class="replaceable"><code>val1</code></em>,<em class="replaceable"><code>val2</code></em>'</code><font style="vertical-align: inherit;"></font><code class="literal">'<em class="replaceable"><code>val2</code></em>,<em class="replaceable"><code>val1</code></em>'</code><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        要确定</font></font><code class="literal">SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        列的</font><font style="vertical-align: inherit;">所有可能值</font><font style="vertical-align: inherit;">，请使用</font><font style="vertical-align: inherit;">并解析
         </font><font style="vertical-align: inherit;">
        输出列中的</font><font style="vertical-align: inherit;">定义</font><font style="vertical-align: inherit;">。
      </font></font><code class="literal">SHOW COLUMNS FROM
        <em class="replaceable"><code>tbl_name</code></em> LIKE
        <em class="replaceable"><code>set_col</code></em></code><font style="vertical-align: inherit;"></font><code class="literal">SET</code><font style="vertical-align: inherit;"></font><code class="literal">Type</code><font style="vertical-align: inherit;"></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在C API中，</font></font><code class="literal">SET</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值以字符串形式返回。</font><font style="vertical-align: inherit;">有关使用结果集元数据将其与其他字符串区分开的信息，请参见
         </font></font><a class="xref" href="connectors-apis.html#c-api-data-structures" title="28.7.5 C API数据结构"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第28.7.5节“C API数据结构”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="spatial-types"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5空间数据类型</font></font></h2>

</div>

</div>

</div>
<div class="toc">
<dl class="toc"><dt><span class="section"><a href="data-types.html#spatial-type-overview"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.1空间数据类型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#opengis-geometry-model"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.2 OpenGIS几何模型</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#gis-data-formats"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.3支持的空间数据格式</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#geometry-well-formedness-validity"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.4几何形状良好和有效性</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#spatial-reference-systems"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.5空间参考系统支持</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#creating-spatial-columns"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.6创建空间列</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#populating-spatial-columns"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.7填充空间列</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#fetching-spatial-data"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.8获取空间数据</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#optimizing-spatial-analysis"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.9优化空间分析</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#creating-spatial-indexes"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.10创建空间索引</font></font></a></span></dt><dt><span class="section"><a href="data-types.html#using-spatial-indexes"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.11使用空间索引</font></font></a></span></dt></dl>
</div>
<a class="indexterm" name="idm140091692650032"></a><a class="indexterm" name="idm140091692648960"></a><a class="indexterm" name="idm140091692647872"></a><a class="indexterm" name="idm140091692646784"></a><a class="indexterm" name="idm140091692645712"></a><a class="indexterm" name="idm140091692644624"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      该</font></font><a class="ulink" href="http://www.opengeospatial.org" target="_top"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">开放地理空间联盟</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（OGC）超过250家企业，机构，以及参与公开可用的概念解决方案，可与各种用来管理空间数据的应用程序非常有用的发展大学的国际财团。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      Open Geospatial Consortium发布
       </font></font><em class="citetitle"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">OpenGIS®地理信息实施标准 - 简单的功能访问 - 第2部分：SQL选项</font></font></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，该文档提出了几种扩展SQL RDBMS以支持空间数据的概念方法。</font><font style="vertical-align: inherit;">该规范可从OGC网站
       </font></font><a class="ulink" href="http://www.opengeospatial.org/standards/sfs" target="_top"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">http://www.opengeospatial.org/standards/sfs获得</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      遵循OGC规范，MySQL将空间扩展实现为</font></font><span class="bold"><strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">具有几何类型</font></font></strong></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">环境</font><font style="vertical-align: inherit;">的</font><span class="bold"><strong><font style="vertical-align: inherit;">SQL</font></strong></span><font style="vertical-align: inherit;">的子集</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">该术语指的是使用一组几何类型扩展的SQL环境。</font><font style="vertical-align: inherit;">几何值SQL列实现为具有几何类型的列。</font><font style="vertical-align: inherit;">该规范描述了一组SQL几何类型，以及这些类型上用于创建和分析几何值的函数。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      MySQL空间扩展支持地理特征的生成，存储和分析：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          用于表示空间值的数据类型
        </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          用于操纵空间值的函数
        </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          空间索引可改善空间列的访问时间
</font></font></p></li></ul>
</div>
<p>
      The spatial data types and functions are available for
      <a class="link" href="storage-engines.html#myisam-storage-engine" title="16.2 MyISAM存储引擎"><code class="literal">MyISAM</code></a>,
      <a class="link" href="innodb-storage-engine.html" title="第15章InnoDB存储引擎"><code class="literal">InnoDB</code></a>,
      <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a>, and
      <a class="link" href="storage-engines.html#archive-storage-engine" title="16.5 ARCHIVE存储引擎"><code class="literal">ARCHIVE</code></a> tables. For indexing spatial
      columns, <code class="literal">MyISAM</code> and <code class="literal">InnoDB</code>
      support both <code class="literal">SPATIAL</code> and
      non-<code class="literal">SPATIAL</code> indexes. The other storage engines
      support non-<code class="literal">SPATIAL</code> indexes, as described in
      <a class="xref" href="sql-syntax.html#create-index" title="13.1.15 CREATE INDEX语法">Section&nbsp;13.1.15, “CREATE INDEX Syntax”</a>.
    </p><a class="indexterm" name="idm140091692624256"></a><p>
      A <span class="bold"><strong>geographic feature</strong></span> is anything
      in the world that has a location. A feature can be:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
          An entity. For example, a mountain, a pond, a city.
        </p></li><li class="listitem"><p>
          A space. For example, town district, the tropics.
        </p></li><li class="listitem"><p>
          A definable location. For example, a crossroad, as a
          particular place where two streets intersect.
</p></li></ul>
</div>
<a class="indexterm" name="idm140091692618992"></a><p>
      Some documents use the term <span class="bold"><strong>geospatial
      feature</strong></span> to refer to geographic features.
    </p><a class="indexterm" name="idm140091692616736"></a><p>
      <span class="bold"><strong>Geometry</strong></span> is another word that
      denotes a geographic feature. Originally the word
      <span class="bold"><strong>geometry</strong></span> meant measurement of the
      earth. Another meaning comes from cartography, referring to the
      geometric features that cartographers use to map the world.
    </p><p>
      The discussion here considers these terms synonymous:
      <span class="bold"><strong>geographic feature</strong></span>,
      <span class="bold"><strong>geospatial feature</strong></span>,
      <span class="bold"><strong>feature</strong></span>, or
      <span class="bold"><strong>geometry</strong></span>. The term most commonly
      used is <span class="bold"><strong>geometry</strong></span>, defined as
      <span class="emphasis"><em>a point or an aggregate of points representing anything
      in the world that has a location</em></span>.
    </p><p>
      The following material covers these topics:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
          The spatial data types implemented in MySQL model
        </p></li><li class="listitem"><p>
          The basis of the spatial extensions in the OpenGIS geometry
          model
        </p></li><li class="listitem"><p>
          Data formats for representing spatial data
        </p></li><li class="listitem"><p>
          How to use spatial data in MySQL
        </p></li><li class="listitem"><p>
          Use of indexing for spatial data
        </p></li><li class="listitem"><p>
          MySQL differences from the OpenGIS specification
</p></li></ul>
</div>
<p>
      For information about functions that operate on spatial data, see
      <a class="xref" href="functions.html#spatial-analysis-functions" title="12.16空间分析功能">Section&nbsp;12.16, “Spatial Analysis Functions”</a>.
</p>
<h3><a name="idm140091692601696"></a>Additional Resources</h3>
<p>
      These standards are important for the MySQL implementation of
      spatial operations:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
          SQL/MM Part 3: Spatial.
        </p></li><li class="listitem"><p>
          The <a class="ulink" href="http://www.opengeospatial.org" target="_top">Open Geospatial
          Consortium</a> publishes the <em class="citetitle">OpenGIS®
          Implementation Standard for Geographic
          information</em>, a document that proposes several
          conceptual ways for extending an SQL RDBMS to support spatial
          data. See in particular Simple Feature Access - Part 1: Common
          Architecture, and Simple Feature Access - Part 2: SQL Option.
          The Open Geospatial Consortium (OGC) maintains a website at
          <a class="ulink" href="http://www.opengeospatial.org/" target="_top">http://www.opengeospatial.org/</a>. The
          specification is available there at
          <a class="ulink" href="http://www.opengeospatial.org/standards/sfs" target="_top">http://www.opengeospatial.org/standards/sfs</a>. It
          contains additional information relevant to the material here.
        </p></li><li class="listitem"><p>
          The grammar for
          <a class="link" href="data-types.html#spatial-reference-systems" title="11.5.5空间参考系统支持">spatial reference
          system</a> (SRS) definitions is based on the grammar
          defined in <em class="citetitle">OpenGIS Implementation Specification:
          Coordinate Transformation Services</em>, Revision 1.00,
          OGC 01-009, January 12, 2001, Section 7.2. This specification
          is available at
          <a class="ulink" href="http://www.opengeospatial.org/standards/ct" target="_top">http://www.opengeospatial.org/standards/ct</a>. For
          differences from that specification in SRS definitions as
          implemented in MySQL, see
          <a class="xref" href="sql-syntax.html#create-spatial-reference-system" title="13.1.19创建空间参考系统语法">Section&nbsp;13.1.19, “CREATE SPATIAL REFERENCE SYSTEM Syntax”</a>.
</p></li></ul>
</div>
<p>
      If you have questions or concerns about the use of the spatial
      extensions to MySQL, you can discuss them in the GIS forum:
      <a class="ulink" href="https://forums.mysql.com/list.php?23" target="_top">https://forums.mysql.com/list.php?23</a>.
</p>
<div class="section">

<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="spatial-type-overview"></a>11.5.1&nbsp;Spatial Data Types</h3>
</div>
</div>
</div>
<a class="indexterm" name="idm140091692589248"></a><a class="indexterm" name="idm140091692588176"></a><a class="indexterm" name="idm140091692587104"></a><a class="indexterm" name="idm140091692586032"></a><a class="indexterm" name="idm140091692584960"></a><a class="indexterm" name="idm140091692583888"></a><a class="indexterm" name="idm140091692582800"></a><a class="indexterm" name="idm140091692581728"></a><a class="indexterm" name="idm140091692580640"></a><a class="indexterm" name="idm140091692579152"></a><a class="indexterm" name="idm140091692577664"></a><a class="indexterm" name="idm140091692576176"></a><a class="indexterm" name="idm140091692574688"></a><a class="indexterm" name="idm140091692573200"></a><a class="indexterm" name="idm140091692571712"></a><a class="indexterm" name="idm140091692570224"></a><a class="indexterm" name="idm140091692568736"></a><a class="indexterm" name="idm140091692567248"></a><p>
        MySQL has spatial data types that correspond to OpenGIS classes.
        The basis for these types is described in
        <a class="xref" href="data-types.html#opengis-geometry-model" title="11.5.2 OpenGIS几何模型">Section&nbsp;11.5.2, “The OpenGIS Geometry Model”</a>.
      </p><p>
        Some spatial data types hold single geometry values:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">GEOMETRY</code>
          </p></li><li class="listitem"><p>
            <code class="literal">POINT</code>
          </p></li><li class="listitem"><p>
            <code class="literal">LINESTRING</code>
          </p></li><li class="listitem"><p>
            <code class="literal">POLYGON</code>
</p></li></ul>
</div>
<p>
        <code class="literal">GEOMETRY</code> can store geometry values of any
        type. The other single-value types (<code class="literal">POINT</code>,
        <code class="literal">LINESTRING</code>, and <code class="literal">POLYGON</code>)
        restrict their values to a particular geometry type.
      </p><p>
        The other spatial data types hold collections of values:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">MULTIPOINT</code>
          </p></li><li class="listitem"><p>
            <code class="literal">MULTILINESTRING</code>
          </p></li><li class="listitem"><p>
            <code class="literal">MULTIPOLYGON</code>
          </p></li><li class="listitem"><p>
            <code class="literal">GEOMETRYCOLLECTION</code>
</p></li></ul>
</div>
<p>
        <code class="literal">GEOMETRYCOLLECTION</code> can store a collection of
        objects of any type. The other collection types
        (<code class="literal">MULTIPOINT</code>,
        <code class="literal">MULTILINESTRING</code>, and
        <code class="literal">MULTIPOLYGON</code>) restrict collection members to
        those having a particular geometry type.
      </p><p>
        Example: To create a table named <code class="literal">geom</code> that
        has a column named <code class="literal">g</code> that can store values of
        any geometry type, use this statement:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE geom (g GEOMETRY);
</pre><p>
        Columns with a spatial data type can have an
        <code class="literal">SRID</code> attribute, to explicitly indicate the
        spatial reference system (SRS) for values stored in the column.
        For example:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE geom (<font></font>
    p POINT SRID 0,<font></font>
    g GEOMETRY NOT NULL SRID 4326<font></font>
);<font></font>
</pre><p>
        <code class="literal">SPATIAL</code> indexes can be created on spatial
        columns if they are <code class="literal">NOT NULL</code> and have a
        specific SRID, so if you plan to index the column, declare it
        with the <code class="literal">NOT NULL</code> and <code class="literal">SRID</code>
        attributes:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE geom (g GEOMETRY NOT NULL SRID 4326);
</pre><p>
        <code class="literal">InnoDB</code> tables permit <code class="literal">SRID</code>
        values for Cartesian and geographic SRSs.
        <code class="literal">MyISAM</code> tables permit <code class="literal">SRID</code>
        values for Cartesian SRSs.
      </p><p>
        The <code class="literal">SRID</code> attribute makes a spatial column
        SRID-restricted, which has these implications:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            The column can contain only values with the given SRID.
            Attempts to insert values with a different SRID produce an
            error.
          </p></li><li class="listitem"><p>
            The optimizer can use <code class="literal">SPATIAL</code> indexes on
            the column. See
            <a class="xref" href="optimization.html#spatial-index-optimization" title="8.3.3空间索引优化">Section&nbsp;8.3.3, “SPATIAL Index Optimization”</a>.
</p></li></ul>
</div>
<p>
        Spatial columns with no <code class="literal">SRID</code> attribute are
        not SRID-restricted and accept values with any SRID. However,
        the optimizer cannot use <code class="literal">SPATIAL</code> indexes on
        them until the column definition is modified to include an
        <code class="literal">SRID</code> attribute, which may require that the
        column contents first be modified so that all values have the
        same SRID.
      </p><p>
        For other examples showing how to use spatial data types in
        MySQL, see <a class="xref" href="data-types.html#creating-spatial-columns" title="11.5.6创建空间列">Section&nbsp;11.5.6, “Creating Spatial Columns”</a>. For
        information about spatial reference systems, see
        <a class="xref" href="data-types.html#spatial-reference-systems" title="11.5.5空间参考系统支持">Section&nbsp;11.5.5, “Spatial Reference System Support”</a>.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="opengis-geometry-model"></a>11.5.2&nbsp;The OpenGIS Geometry Model</h3>

</div>

</div>

</div>
<div class="toc">
<dl class="toc"><dt><span class="section"><a href="data-types.html#gis-geometry-class-hierarchy">11.5.2.1 The Geometry Class Hierarchy</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-geometry">11.5.2.2 Geometry Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-point">11.5.2.3 Point Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-curve">11.5.2.4 Curve Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-linestring">11.5.2.5 LineString Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-surface">11.5.2.6 Surface Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-polygon">11.5.2.7 Polygon Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-geometrycollection">11.5.2.8 GeometryCollection Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-multipoint">11.5.2.9 MultiPoint Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-multicurve">11.5.2.10 MultiCurve Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-multilinestring">11.5.2.11 MultiLineString Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-multisurface">11.5.2.12 MultiSurface Class</a></span></dt><dt><span class="section"><a href="data-types.html#gis-class-multipolygon">11.5.2.13 MultiPolygon Class</a></span></dt></dl>
</div>
<p>
        The set of geometry types proposed by OGC's
        <span class="bold"><strong>SQL with Geometry Types</strong></span>
        environment is based on the <span class="bold"><strong>OpenGIS
        Geometry Model</strong></span>. In this model, each geometric object
        has the following general properties:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            It is associated with a spatial reference system, which
            describes the coordinate space in which the object is
            defined.
          </p></li><li class="listitem"><p>
            It belongs to some geometry class.
</p></li></ul>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-geometry-class-hierarchy"></a>11.5.2.1&nbsp;The Geometry Class Hierarchy</h4>

</div>

</div>

</div>
<p>
          The geometry classes define a hierarchy as follows:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              <code class="literal">Geometry</code> (noninstantiable)
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                  <code class="literal">Point</code> (instantiable)
                </p></li><li class="listitem"><p>
                  <code class="literal">Curve</code> (noninstantiable)
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: square; "><li class="listitem"><p>
                      <code class="literal">LineString</code> (instantiable)
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
                          <code class="literal">Line</code>
                        </p></li><li class="listitem"><p>
                          <code class="literal">LinearRing</code>
</p></li></ul>
</div>
</li></ul>
</div>
</li><li class="listitem"><p>
                  <code class="literal">Surface</code> (noninstantiable)
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: square; "><li class="listitem"><p>
                      <code class="literal">Polygon</code> (instantiable)
</p></li></ul>
</div>
</li><li class="listitem"><p>
                  <code class="literal">GeometryCollection</code> (instantiable)
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: square; "><li class="listitem"><p>
                      <code class="literal">MultiPoint</code> (instantiable)
                    </p></li><li class="listitem"><p>
                      <code class="literal">MultiCurve</code> (noninstantiable)
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
                          <code class="literal">MultiLineString</code>
                          (instantiable)
</p></li></ul>
</div>
</li><li class="listitem"><p>
                      <code class="literal">MultiSurface</code> (noninstantiable)
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
                          <code class="literal">MultiPolygon</code> (instantiable)
</p></li></ul>
</div>
</li></ul>
</div>
</li></ul>
</div>
</li></ul>
</div>
<p>
          It is not possible to create objects in noninstantiable
          classes. It is possible to create objects in instantiable
          classes. All classes have properties, and instantiable classes
          may also have assertions (rules that define valid class
          instances).
        </p><p>
          <code class="literal">Geometry</code> is the base class. It is an
          abstract class. The instantiable subclasses of
          <code class="literal">Geometry</code> are restricted to zero-, one-, and
          two-dimensional geometric objects that exist in
          two-dimensional coordinate space. All instantiable geometry
          classes are defined so that valid instances of a geometry
          class are topologically closed (that is, all defined
          geometries include their boundary).
        </p><p>
          The base <code class="literal">Geometry</code> class has subclasses for
          <code class="literal">Point</code>, <code class="literal">Curve</code>,
          <code class="literal">Surface</code>, and
          <code class="literal">GeometryCollection</code>:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              <code class="literal">Point</code> represents zero-dimensional
              objects.
            </p></li><li class="listitem"><p>
              <code class="literal">Curve</code> represents one-dimensional
              objects, and has subclass <code class="literal">LineString</code>,
              with sub-subclasses <code class="literal">Line</code> and
              <code class="literal">LinearRing</code>.
            </p></li><li class="listitem"><p>
              <code class="literal">Surface</code> is designed for two-dimensional
              objects and has subclass <code class="literal">Polygon</code>.
            </p></li><li class="listitem"><p>
              <code class="literal">GeometryCollection</code> has specialized
              zero-, one-, and two-dimensional collection classes named
              <code class="literal">MultiPoint</code>,
              <code class="literal">MultiLineString</code>, and
              <code class="literal">MultiPolygon</code> for modeling geometries
              corresponding to collections of <code class="literal">Points</code>,
              <code class="literal">LineStrings</code>, and
              <code class="literal">Polygons</code>, respectively.
              <code class="literal">MultiCurve</code> and
              <code class="literal">MultiSurface</code> are introduced as abstract
              superclasses that generalize the collection interfaces to
              handle <code class="literal">Curves</code> and
              <code class="literal">Surfaces</code>.
</p></li></ul>
</div>
<p>
          <code class="literal">Geometry</code>, <code class="literal">Curve</code>,
          <code class="literal">Surface</code>, <code class="literal">MultiCurve</code>, and
          <code class="literal">MultiSurface</code> are defined as noninstantiable
          classes. They define a common set of methods for their
          subclasses and are included for extensibility.
        </p><p>
          <code class="literal">Point</code>, <code class="literal">LineString</code>,
          <code class="literal">Polygon</code>,
          <code class="literal">GeometryCollection</code>,
          <code class="literal">MultiPoint</code>,
          <code class="literal">MultiLineString</code>, and
          <code class="literal">MultiPolygon</code> are instantiable classes.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-geometry"></a>11.5.2.2&nbsp;Geometry Class</h4>

</div>

</div>

</div>
<p>
          <code class="literal">Geometry</code> is the root class of the
          hierarchy. It is a noninstantiable class but has a number of
          properties, described in the following list, that are common
          to all geometry values created from any of the
          <code class="literal">Geometry</code> subclasses. Particular subclasses
          have their own specific properties, described later.
        </p><p>
          <span class="bold"><strong>Geometry Properties</strong></span>
        </p><p>
          A geometry value has the following properties:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              Its <span class="bold"><strong>type</strong></span>. Each geometry
              belongs to one of the instantiable classes in the
              hierarchy.
            </p></li><li class="listitem"><p>
              Its <span class="bold"><strong>SRID</strong></span>, or spatial
              reference identifier. This value identifies the geometry's
              associated spatial reference system that describes the
              coordinate space in which the geometry object is defined.
            </p><p>
              In MySQL, the SRID value is an integer associated with the
              geometry value. The maximum usable SRID value is
              2<sup>32</sup>−1. If a larger value
              is given, only the lower 32 bits are used.
            </p><p>
              SRID 0 represents an infinite flat Cartesian plane with no
              units assigned to its axes. To ensure SRID 0 behavior,
              create geometry values using SRID 0. SRID 0 is the default
              for new geometry values if no SRID is specified.
            </p><p>
              For computations on multiple geometry values, all values
              must have the same SRID or an error occurs.
            </p></li><li class="listitem"><p>
              Its <span class="bold"><strong>coordinates</strong></span> in its
              spatial reference system, represented as double-precision
              (8-byte) numbers. All nonempty geometries include at least
              one pair of (X,Y) coordinates. Empty geometries contain no
              coordinates.
            </p><p>
              Coordinates are related to the SRID. For example, in
              different coordinate systems, the distance between two
              objects may differ even when objects have the same
              coordinates, because the distance on the
              <span class="bold"><strong>planar</strong></span> coordinate system
              and the distance on the
              <span class="bold"><strong>geodetic</strong></span> system
              (coordinates on the Earth's surface) are different things.
            </p></li><li class="listitem"><p>
              Its <span class="bold"><strong>interior</strong></span>,
              <span class="bold"><strong>boundary</strong></span>, and
              <span class="bold"><strong>exterior</strong></span>.
            </p><p>
              Every geometry occupies some position in space. The
              exterior of a geometry is all space not occupied by the
              geometry. The interior is the space occupied by the
              geometry. The boundary is the interface between the
              geometry's interior and exterior.
            </p></li><li class="listitem"><p>
              Its <span class="bold"><strong>MBR</strong></span> (minimum bounding
              rectangle), or envelope. This is the bounding geometry,
              formed by the minimum and maximum (X,Y) coordinates:
            </p><pre data-lang="simple" class="programlisting">((MINX MINY, MAXX MINY, MAXX MAXY, MINX MAXY, MINX MINY))
</pre></li><li class="listitem"><p>
              Whether the value is
              <span class="bold"><strong>simple</strong></span> or
              <span class="bold"><strong>nonsimple</strong></span>. Geometry
              values of types (<code class="literal">LineString</code>,
              <code class="literal">MultiPoint</code>,
              <code class="literal">MultiLineString</code>) are either simple or
              nonsimple. Each type determines its own assertions for
              being simple or nonsimple.
            </p></li><li class="listitem"><p>
              Whether the value is
              <span class="bold"><strong>closed</strong></span> or
              <span class="bold"><strong>not closed</strong></span>. Geometry
              values of types (<code class="literal">LineString</code>,
              <code class="literal">MultiString</code>) are either closed or not
              closed. Each type determines its own assertions for being
              closed or not closed.
            </p></li><li class="listitem"><p>
              Whether the value is
              <span class="bold"><strong>empty</strong></span> or
              <span class="bold"><strong>nonempty</strong></span> A geometry is
              empty if it does not have any points. Exterior, interior,
              and boundary of an empty geometry are not defined (that
              is, they are represented by a <code class="literal">NULL</code>
              value). An empty geometry is defined to be always simple
              and has an area of 0.
            </p></li><li class="listitem"><p>
              Its <span class="bold"><strong>dimension</strong></span>. A geometry
              can have a dimension of −1, 0, 1, or 2:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                  −1 for an empty geometry.
                </p></li><li class="listitem"><p>
                  0 for a geometry with no length and no area.
                </p></li><li class="listitem"><p>
                  1 for a geometry with nonzero length and zero area.
                </p></li><li class="listitem"><p>
                  2 for a geometry with nonzero area.
</p></li></ul>
</div>
<p>
              <code class="literal">Point</code> objects have a dimension of zero.
              <code class="literal">LineString</code> objects have a dimension of
              1. <code class="literal">Polygon</code> objects have a dimension of
              2. The dimensions of <code class="literal">MultiPoint</code>,
              <code class="literal">MultiLineString</code>, and
              <code class="literal">MultiPolygon</code> objects are the same as
              the dimensions of the elements they consist of.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-point"></a>11.5.2.3&nbsp;Point Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">Point</code> is a geometry that represents a
          single location in coordinate space.
        </p><p>
          <span class="bold"><strong><code class="literal">Point</code>
          Examples</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              Imagine a large-scale map of the world with many cities. A
              <code class="literal">Point</code> object could represent each city.
            </p></li><li class="listitem"><p>
              On a city map, a <code class="literal">Point</code> object could
              represent a bus stop.
</p></li></ul>
</div>
<p>
          <span class="bold"><strong><code class="literal">Point</code>
          Properties</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              X-coordinate value.
            </p></li><li class="listitem"><p>
              Y-coordinate value.
            </p></li><li class="listitem"><p>
              <code class="literal">Point</code> is defined as a zero-dimensional
              geometry.
            </p></li><li class="listitem"><p>
              The boundary of a <code class="literal">Point</code> is the empty
              set.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-curve"></a>11.5.2.4&nbsp;Curve Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">Curve</code> is a one-dimensional geometry,
          usually represented by a sequence of points. Particular
          subclasses of <code class="literal">Curve</code> define the type of
          interpolation between points. <code class="literal">Curve</code> is a
          noninstantiable class.
        </p><p>
          <span class="bold"><strong><code class="literal">Curve</code>
          Properties</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              A <code class="literal">Curve</code> has the coordinates of its
              points.
            </p></li><li class="listitem"><p>
              A <code class="literal">Curve</code> is defined as a one-dimensional
              geometry.
            </p></li><li class="listitem"><p>
              A <code class="literal">Curve</code> is simple if it does not pass
              through the same point twice, with the exception that a
              curve can still be simple if the start and end points are
              the same.
            </p></li><li class="listitem"><p>
              A <code class="literal">Curve</code> is closed if its start point is
              equal to its endpoint.
            </p></li><li class="listitem"><p>
              The boundary of a closed <code class="literal">Curve</code> is
              empty.
            </p></li><li class="listitem"><p>
              The boundary of a nonclosed <code class="literal">Curve</code>
              consists of its two endpoints.
            </p></li><li class="listitem"><p>
              A <code class="literal">Curve</code> that is simple and closed is a
              <code class="literal">LinearRing</code>.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-linestring"></a>11.5.2.5&nbsp;LineString Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">LineString</code> is a <code class="literal">Curve</code>
          with linear interpolation between points.
        </p><p>
          <span class="bold"><strong><code class="literal">LineString</code>
          Examples</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              On a world map, <code class="literal">LineString</code> objects
              could represent rivers.
            </p></li><li class="listitem"><p>
              In a city map, <code class="literal">LineString</code> objects could
              represent streets.
</p></li></ul>
</div>
<p>
          <span class="bold"><strong><code class="literal">LineString</code>
          Properties</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              A <code class="literal">LineString</code> has coordinates of
              segments, defined by each consecutive pair of points.
            </p></li><li class="listitem"><p>
              A <code class="literal">LineString</code> is a
              <code class="literal">Line</code> if it consists of exactly two
              points.
            </p></li><li class="listitem"><p>
              A <code class="literal">LineString</code> is a
              <code class="literal">LinearRing</code> if it is both closed and
              simple.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-surface"></a>11.5.2.6&nbsp;Surface Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">Surface</code> is a two-dimensional geometry. It
          is a noninstantiable class. Its only instantiable subclass is
          <code class="literal">Polygon</code>.
        </p><p>
          <span class="bold"><strong><code class="literal">Surface</code>
          Properties</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              A <code class="literal">Surface</code> is defined as a
              two-dimensional geometry.
            </p></li><li class="listitem"><p>
              The OpenGIS specification defines a simple
              <code class="literal">Surface</code> as a geometry that consists of
              a single <span class="quote">“<span class="quote">patch</span>”</span> that is associated with a
              single exterior boundary and zero or more interior
              boundaries.
            </p></li><li class="listitem"><p>
              The boundary of a simple <code class="literal">Surface</code> is the
              set of closed curves corresponding to its exterior and
              interior boundaries.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-polygon"></a>11.5.2.7&nbsp;Polygon Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">Polygon</code> is a planar
          <code class="literal">Surface</code> representing a multisided geometry.
          It is defined by a single exterior boundary and zero or more
          interior boundaries, where each interior boundary defines a
          hole in the <code class="literal">Polygon</code>.
        </p><p>
          <span class="bold"><strong><code class="literal">Polygon</code>
          Examples</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              On a region map, <code class="literal">Polygon</code> objects could
              represent forests, districts, and so on.
</p></li></ul>
</div>
<p>
          <span class="bold"><strong><code class="literal">Polygon</code>
          Assertions</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              The boundary of a <code class="literal">Polygon</code> consists of a
              set of <code class="literal">LinearRing</code> objects (that is,
              <code class="literal">LineString</code> objects that are both simple
              and closed) that make up its exterior and interior
              boundaries.
            </p></li><li class="listitem"><p>
              A <code class="literal">Polygon</code> has no rings that cross. The
              rings in the boundary of a <code class="literal">Polygon</code> may
              intersect at a <code class="literal">Point</code>, but only as a
              tangent.
            </p></li><li class="listitem"><p>
              A <code class="literal">Polygon</code> has no lines, spikes, or
              punctures.
            </p></li><li class="listitem"><p>
              A <code class="literal">Polygon</code> has an interior that is a
              connected point set.
            </p></li><li class="listitem"><p>
              A <code class="literal">Polygon</code> may have holes. The exterior
              of a <code class="literal">Polygon</code> with holes is not
              connected. Each hole defines a connected component of the
              exterior.
</p></li></ul>
</div>
<p>
          The preceding assertions make a <code class="literal">Polygon</code> a
          simple geometry.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-geometrycollection"></a>11.5.2.8&nbsp;GeometryCollection Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">GeomCollection</code> is a geometry that is a
          collection of zero or more geometries of any class.
        </p><p>
          <code class="literal">GeomCollection</code> and
          <code class="literal">GeometryCollection</code> are synonymous, with
          <code class="literal">GeomCollection</code> the preferred type name.
        </p><p>
          All the elements in a geometry collection must be in the same
          spatial reference system (that is, in the same coordinate
          system). There are no other constraints on the elements of a
          geometry collection, although the subclasses of
          <code class="literal">GeomCollection</code> described in the following
          sections may restrict membership. Restrictions may be based
          on:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              Element type (for example, a <code class="literal">MultiPoint</code>
              may contain only <code class="literal">Point</code> elements)
            </p></li><li class="listitem"><p>
              Dimension
            </p></li><li class="listitem"><p>
              Constraints on the degree of spatial overlap between
              elements
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-multipoint"></a>11.5.2.9&nbsp;MultiPoint Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">MultiPoint</code> is a geometry collection
          composed of <code class="literal">Point</code> elements. The points are
          not connected or ordered in any way.
        </p><p>
          <span class="bold"><strong><code class="literal">MultiPoint</code>
          Examples</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              On a world map, a <code class="literal">MultiPoint</code> could
              represent a chain of small islands.
            </p></li><li class="listitem"><p>
              On a city map, a <code class="literal">MultiPoint</code> could
              represent the outlets for a ticket office.
</p></li></ul>
</div>
<p>
          <span class="bold"><strong><code class="literal">MultiPoint</code>
          Properties</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              A <code class="literal">MultiPoint</code> is a zero-dimensional
              geometry.
            </p></li><li class="listitem"><p>
              A <code class="literal">MultiPoint</code> is simple if no two of its
              <code class="literal">Point</code> values are equal (have identical
              coordinate values).
            </p></li><li class="listitem"><p>
              The boundary of a <code class="literal">MultiPoint</code> is the
              empty set.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-multicurve"></a>11.5.2.10&nbsp;MultiCurve Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">MultiCurve</code> is a geometry collection
          composed of <code class="literal">Curve</code> elements.
          <code class="literal">MultiCurve</code> is a noninstantiable class.
        </p><p>
          <span class="bold"><strong><code class="literal">MultiCurve</code>
          Properties</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              A <code class="literal">MultiCurve</code> is a one-dimensional
              geometry.
            </p></li><li class="listitem"><p>
              A <code class="literal">MultiCurve</code> is simple if and only if
              all of its elements are simple; the only intersections
              between any two elements occur at points that are on the
              boundaries of both elements.
            </p></li><li class="listitem"><p>
              A <code class="literal">MultiCurve</code> boundary is obtained by
              applying the <span class="quote">“<span class="quote">mod 2 union rule</span>”</span> (also known
              as the <span class="quote">“<span class="quote">odd-even rule</span>”</span>): A point is in the
              boundary of a <code class="literal">MultiCurve</code> if it is in
              the boundaries of an odd number of
              <code class="literal">Curve</code> elements.
            </p></li><li class="listitem"><p>
              A <code class="literal">MultiCurve</code> is closed if all of its
              elements are closed.
            </p></li><li class="listitem"><p>
              The boundary of a closed <code class="literal">MultiCurve</code> is
              always empty.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-multilinestring"></a>11.5.2.11&nbsp;MultiLineString Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">MultiLineString</code> is a
          <code class="literal">MultiCurve</code> geometry collection composed of
          <code class="literal">LineString</code> elements.
        </p><p>
          <span class="bold"><strong><code class="literal">MultiLineString</code>
          Examples</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              On a region map, a <code class="literal">MultiLineString</code>
              could represent a river system or a highway system.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-multisurface"></a>11.5.2.12&nbsp;MultiSurface Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">MultiSurface</code> is a geometry collection
          composed of surface elements. <code class="literal">MultiSurface</code>
          is a noninstantiable class. Its only instantiable subclass is
          <code class="literal">MultiPolygon</code>.
        </p><p>
          <span class="bold"><strong><code class="literal">MultiSurface</code>
          Assertions</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              Surfaces within a <code class="literal">MultiSurface</code> have no
              interiors that intersect.
            </p></li><li class="listitem"><p>
              Surfaces within a <code class="literal">MultiSurface</code> have
              boundaries that intersect at most at a finite number of
              points.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="gis-class-multipolygon"></a>11.5.2.13&nbsp;MultiPolygon Class</h4>

</div>

</div>

</div>
<p>
          A <code class="literal">MultiPolygon</code> is a
          <code class="literal">MultiSurface</code> object composed of
          <code class="literal">Polygon</code> elements.
        </p><p>
          <span class="bold"><strong><code class="literal">MultiPolygon</code>
          Examples</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              On a region map, a <code class="literal">MultiPolygon</code> could
              represent a system of lakes.
</p></li></ul>
</div>
<p>
          <span class="bold"><strong><code class="literal">MultiPolygon</code>
          Assertions</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              A <code class="literal">MultiPolygon</code> has no two
              <code class="literal">Polygon</code> elements with interiors that
              intersect.
            </p></li><li class="listitem"><p>
              A <code class="literal">MultiPolygon</code> has no two
              <code class="literal">Polygon</code> elements that cross (crossing
              is also forbidden by the previous assertion), or that
              touch at an infinite number of points.
            </p></li><li class="listitem"><p>
              A <code class="literal">MultiPolygon</code> may not have cut lines,
              spikes, or punctures. A <code class="literal">MultiPolygon</code> is
              a regular, closed point set.
            </p></li><li class="listitem"><p>
              A <code class="literal">MultiPolygon</code> that has more than one
              <code class="literal">Polygon</code> has an interior that is not
              connected. The number of connected components of the
              interior of a <code class="literal">MultiPolygon</code> is equal to
              the number of <code class="literal">Polygon</code> values in the
              <code class="literal">MultiPolygon</code>.
</p></li></ul>
</div>
<p>
          <span class="bold"><strong><code class="literal">MultiPolygon</code>
          Properties</strong></span>
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              A <code class="literal">MultiPolygon</code> is a two-dimensional
              geometry.
            </p></li><li class="listitem"><p>
              A <code class="literal">MultiPolygon</code> boundary is a set of
              closed curves (<code class="literal">LineString</code> values)
              corresponding to the boundaries of its
              <code class="literal">Polygon</code> elements.
            </p></li><li class="listitem"><p>
              Each <code class="literal">Curve</code> in the boundary of the
              <code class="literal">MultiPolygon</code> is in the boundary of
              exactly one <code class="literal">Polygon</code> element.
            </p></li><li class="listitem"><p>
              Every <code class="literal">Curve</code> in the boundary of an
              <code class="literal">Polygon</code> element is in the boundary of
              the <code class="literal">MultiPolygon</code>.
</p></li></ul>
</div>

</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="gis-data-formats"></a>11.5.3&nbsp;Supported Spatial Data Formats</h3>

</div>

</div>

</div>
<p>
        Two standard spatial data formats are used to represent geometry
        objects in queries:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Well-Known Text (WKT) format
          </p></li><li class="listitem"><p>
            Well-Known Binary (WKB) format
</p></li></ul>
</div>
<p>
        Internally, MySQL stores geometry values in a format that is not
        identical to either WKT or WKB format. (Internal format is like
        WKB but with an initial 4 bytes to indicate the SRID.)
      </p><p>
        There are functions available to convert between different data
        formats; see <a class="xref" href="functions.html#gis-format-conversion-functions" title="12.16.6几何格式转换函数">Section&nbsp;12.16.6, “Geometry Format Conversion Functions”</a>.
      </p><p>
        The following sections describe the spatial data formats MySQL
        uses:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><a class="xref" href="data-types.html#gis-wkt-format" title="熟知文本（WKT）格式">Well-Known Text (WKT) Format</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#gis-wkb-format" title="众所周知的二进制（WKB）格式">Well-Known Binary (WKB) Format</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#gis-internal-format" title="内部几何存储格式">Internal Geometry Storage Format</a></p></li></ul>
</div>

<div class="simplesect">

<div class="titlepage">
<div>

<div class="simple">
<h4 class="title"><a name="gis-wkt-format"></a>Well-Known Text (WKT) Format</h4>

</div>

</div>

</div>
<a class="indexterm" name="idm140091692218400"></a><a class="indexterm" name="idm140091692216944"></a><a class="indexterm" name="idm140091692215456"></a><p>
          The Well-Known Text (WKT) representation of geometry values is
          designed for exchanging geometry data in ASCII form. The
          OpenGIS specification provides a Backus-Naur grammar that
          specifies the formal production rules for writing WKT values
          (see <a class="xref" href="data-types.html#spatial-types" title="11.5空间数据类型">Section&nbsp;11.5, “Spatial Data Types”</a>).
        </p><p>
          Examples of WKT representations of geometry objects:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              A <code class="literal">Point</code>:
            </p><pre data-lang="simple" class="programlisting">POINT(15 20)
</pre><p>
              The point coordinates are specified with no separating
              comma. This differs from the syntax for the SQL
              <a class="link" href="functions.html#function_point"><code class="literal">Point()</code></a> function, which
              requires a comma between the coordinates. Take care to use
              the syntax appropriate to the context of a given spatial
              operation. For example, the following statements both use
              <a class="link" href="functions.html#function_st-x"><code class="literal">ST_X()</code></a> to extract the
              X-coordinate from a <code class="literal">Point</code> object. The
              first produces the object directly using the
              <a class="link" href="functions.html#function_point"><code class="literal">Point()</code></a> function. The
              second uses a WKT representation converted to a
              <code class="literal">Point</code> with
              <a class="link" href="functions.html#function_st-geomfromtext"><code class="literal">ST_GeomFromText()</code></a>.
            </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT ST_X(Point(15, 20));</code></strong><font></font>
+---------------------+<font></font>
| ST_X(POINT(15, 20)) |<font></font>
+---------------------+<font></font>
|                  15 |<font></font>
+---------------------+<font></font>
<font></font>
mysql&gt; <strong class="userinput"><code>SELECT ST_X(ST_GeomFromText('POINT(15 20)'));</code></strong><font></font>
+---------------------------------------+<font></font>
| ST_X(ST_GeomFromText('POINT(15 20)')) |<font></font>
+---------------------------------------+<font></font>
|                                    15 |<font></font>
+---------------------------------------+<font></font>
</pre></li><li class="listitem"><p>
              A <code class="literal">LineString</code> with four points:
            </p><pre data-lang="simple" class="programlisting">LINESTRING(0 0, 10 10, 20 25, 50 60)
</pre><p>
              The point coordinate pairs are separated by commas.
            </p></li><li class="listitem"><p>
              A <code class="literal">Polygon</code> with one exterior ring and
              one interior ring:
            </p><pre data-lang="simple" class="programlisting">POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))
</pre></li><li class="listitem"><p>
              A <code class="literal">MultiPoint</code> with three
              <code class="literal">Point</code> values:
            </p><pre data-lang="simple" class="programlisting">MULTIPOINT(0 0, 20 20, 60 60)
</pre><p>
              Spatial functions such as
              <a class="link" href="functions.html#function_st-mpointfromtext"><code class="literal">ST_MPointFromText()</code></a> and
              <a class="link" href="functions.html#function_st-geomfromtext"><code class="literal">ST_GeomFromText()</code></a> that
              accept WKT-format representations of
              <code class="literal">MultiPoint</code> values permit individual
              points within values to be surrounded by parentheses. For
              example, both of the following function calls are valid:
            </p><pre data-lang="sql" class="programlisting">ST_MPointFromText('MULTIPOINT (1 1, 2 2, 3 3)')<font></font>
ST_MPointFromText('MULTIPOINT ((1 1), (2 2), (3 3))')<font></font>
</pre></li><li class="listitem"><p>
              A <code class="literal">MultiLineString</code> with two
              <code class="literal">LineString</code> values:
            </p><pre data-lang="simple" class="programlisting">MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
</pre></li><li class="listitem"><p>
              A <code class="literal">MultiPolygon</code> with two
              <code class="literal">Polygon</code> values:
            </p><pre data-lang="simple" class="programlisting">MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))
</pre></li><li class="listitem"><p>
              A <code class="literal">GeometryCollection</code> consisting of two
              <code class="literal">Point</code> values and one
              <code class="literal">LineString</code>:
            </p><pre data-lang="simple" class="programlisting">GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))
</pre></li></ul>
</div>

</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h4 class="title"><a name="gis-wkb-format"></a>Well-Known Binary (WKB) Format</h4>

</div>

</div>

</div>
<a class="indexterm" name="idm140091692170112"></a><a class="indexterm" name="idm140091692168656"></a><a class="indexterm" name="idm140091692167152"></a><p>
          The Well-Known Binary (WKB) representation of geometric values
          is used for exchanging geometry data as binary streams
          represented by <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a> values
          containing geometric WKB information. This format is defined
          by the OpenGIS specification (see
          <a class="xref" href="data-types.html#spatial-types" title="11.5空间数据类型">Section&nbsp;11.5, “Spatial Data Types”</a>). It is also defined in the
          ISO <em class="citetitle">SQL/MM Part 3: Spatial</em> standard.
        </p><p>
          WKB uses 1-byte unsigned integers, 4-byte unsigned integers,
          and 8-byte double-precision numbers (IEEE 754 format). A byte
          is eight bits.
        </p><p>
          For example, a WKB value that corresponds to <code class="literal">POINT(1
          -1)</code> consists of this sequence of 21 bytes, each
          represented by two hexadecimal digits:
        </p><pre data-lang="simple" class="programlisting">0101000000000000000000F03F000000000000F0BF
</pre><p>
          The sequence consists of the components shown in the following
          table.
</p>
<div class="table">
<a name="wkb-components-example-table"></a><p class="title"><b>Table&nbsp;11.2&nbsp;WKB Components Example</b></p>
<div class="table-contents">
<table summary="Example showing component in WKB values."><colgroup><col width="30%"><col width="30%"><col width="40%"></colgroup><thead><tr>
              <th scope="col">Component</th>
              <th scope="col">Size</th>
              <th scope="col">Value</th>
            </tr></thead><tbody><tr>
              <td align="left" scope="row">Byte order</td>
              <td align="left">1 byte</td>
              <td align="left"><code class="literal">01</code></td>
            </tr><tr>
              <td align="left" scope="row">WKB type</td>
              <td align="left">4 bytes</td>
              <td align="left"><code class="literal">01000000</code></td>
            </tr><tr>
              <td align="left" scope="row">X coordinate</td>
              <td align="left">8 bytes</td>
              <td align="left"><code class="literal">000000000000F03F</code></td>
            </tr><tr>
              <td align="left" scope="row">Y coordinate</td>
              <td align="left">8 bytes</td>
              <td align="left"><code class="literal">000000000000F0BF</code></td>
</tr></tbody></table>
</div>

</div>
<br class="table-break"><p>
          Component representation is as follows:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              The byte order indicator is either 1 or 0 to signify
              little-endian or big-endian storage. The little-endian and
              big-endian byte orders are also known as Network Data
              Representation (NDR) and External Data Representation
              (XDR), respectively.
            </p></li><li class="listitem"><p>
              The WKB type is a code that indicates the geometry type.
              MySQL uses values from 1 through 7 to indicate
              <code class="literal">Point</code>, <code class="literal">LineString</code>,
              <code class="literal">Polygon</code>, <code class="literal">MultiPoint</code>,
              <code class="literal">MultiLineString</code>,
              <code class="literal">MultiPolygon</code>, and
              <code class="literal">GeometryCollection</code>.
            </p></li><li class="listitem"><p>
              A <code class="literal">Point</code> value has X and Y coordinates,
              each represented as a double-precision value.
</p></li></ul>
</div>
<p>
          WKB values for more complex geometry values have more complex
          data structures, as detailed in the OpenGIS specification.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h4 class="title"><a name="gis-internal-format"></a>Internal Geometry Storage Format</h4>

</div>

</div>

</div>
<a class="indexterm" name="idm140091692121472"></a><a class="indexterm" name="idm140091692120016"></a><p>
          MySQL stores geometry values using 4 bytes to indicate the
          SRID followed by the WKB representation of the value. For a
          description of WKB format, see
          <a class="xref" href="data-types.html#gis-wkb-format" title="众所周知的二进制（WKB）格式">Well-Known Binary (WKB) Format</a>.
        </p><p>
          For the WKB part, these MySQL-specific considerations apply:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              The byte-order indicator byte is 1 because MySQL stores
              geometries as little-ending values.
            </p></li><li class="listitem"><p>
              MySQL supports geometry types of <code class="literal">Point</code>,
              <code class="literal">LineString</code>, <code class="literal">Polygon</code>,
              <code class="literal">MultiPoint</code>,
              <code class="literal">MultiLineString</code>,
              <code class="literal">MultiPolygon</code>, and
              <code class="literal">GeometryCollection</code>. Other geometry
              types are not supported.
            </p></li><li class="listitem"><p>
              Only <code class="literal">GeometryCollection</code> can be empty.
              Such a value is stored with 0 elements.
            </p></li><li class="listitem"><p>
              Polygon rings can be specified both clockwise and
              counterclockwise. MySQL flips the rings automatically when
              reading data.
</p></li></ul>
</div>
<p>
          Cartesian coordinates are stored in the length unit of the
          spatial reference system, with X values in the X coordinates
          and Y values in the Y coordinates. Axis directions are those
          specified by the spatial reference system.
        </p><p>
          Geographic coordinates are stored in the angle unit of the
          spatial reference system, with longitudes in the X coordinates
          and latitudes in the Y coordinates. Axis directions and the
          meridian are those specified by the spatial reference system.
        </p><p>
          The <a class="link" href="functions.html#function_length"><code class="literal">LENGTH()</code></a> function returns
          the space in bytes required for value storage. Example:
        </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SET @g = ST_GeomFromText('POINT(1 -1)');</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT LENGTH(@g);</code></strong><font></font>
+------------+<font></font>
| LENGTH(@g) |<font></font>
+------------+<font></font>
|         25 |<font></font>
+------------+<font></font>
mysql&gt; <strong class="userinput"><code>SELECT HEX(@g);</code></strong><font></font>
+----------------------------------------------------+<font></font>
| HEX(@g)                                            |<font></font>
+----------------------------------------------------+<font></font>
| 000000000101000000000000000000F03F000000000000F0BF |<font></font>
+----------------------------------------------------+<font></font>
</pre><p>
          The value length is 25 bytes, made up of these components (as
          can be seen from the hexadecimal value):
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
              4 bytes for integer SRID (0)
            </p></li><li class="listitem"><p>
              1 byte for integer byte order (1 = little-endian)
            </p></li><li class="listitem"><p>
              4 bytes for integer type information (1 =
              <code class="literal">Point</code>)
            </p></li><li class="listitem"><p>
              8 bytes for double-precision X coordinate (1)
            </p></li><li class="listitem"><p>
              8 bytes for double-precision Y coordinate (−1)
</p></li></ul>
</div>

</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="geometry-well-formedness-validity"></a>11.5.4&nbsp;Geometry Well-Formedness and Validity</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091692092880"></a><a class="indexterm" name="idm140091692091376"></a><a class="indexterm" name="idm140091692089872"></a><a class="indexterm" name="idm140091692088384"></a><a class="indexterm" name="idm140091692086896"></a><a class="indexterm" name="idm140091692085408"></a><a class="indexterm" name="idm140091692083920"></a><a class="indexterm" name="idm140091692082432"></a><a class="indexterm" name="idm140091692080944"></a><a class="indexterm" name="idm140091692079440"></a><p>
        For geometry values, MySQL distinguishes between the concepts of
        syntactically well-formed and geometrically valid.
      </p><p>
        A geometry is syntactically well-formed if it satisfies
        conditions such as those in this (nonexhaustive) list:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Linestrings have at least two points
          </p></li><li class="listitem"><p>
            Polygons have at least one ring
          </p></li><li class="listitem"><p>
            Polygon rings are closed (first and last points the same)
          </p></li><li class="listitem"><p>
            Polygon rings have at least 4 points (minimum polygon is a
            triangle with first and last points the same)
          </p></li><li class="listitem"><p>
            Collections are not empty (except
            <code class="literal">GeometryCollection</code>)
</p></li></ul>
</div>
<p>
        A geometry is geometrically valid if it is syntactically
        well-formed and satisfies conditions such as those in this
        (nonexhaustive) list:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Polygons are not self-intersecting
          </p></li><li class="listitem"><p>
            Polygon interior rings are inside the exterior ring
          </p></li><li class="listitem"><p>
            Multipolygons do not have overlapping polygons
</p></li></ul>
</div>
<p>
        Spatial functions fail if a geometry is not syntactically
        well-formed. Spatial import functions that parse WKT or WKB
        values raise an error for attempts to create a geometry that is
        not syntactically well-formed. Syntactic well-formedness is also
        checked for attempts to store geometries into tables.
      </p><p>
        It is permitted to insert, select, and update geometrically
        invalid geometries, but they must be syntactically well-formed.
        Due to the computational expense, MySQL does not check
        explicitly for geometric validity. Spatial computations may
        detect some cases of invalid geometries and raise an error, but
        they may also return an undefined result without detecting the
        invalidity. Applications that require geometically valid
        geometries should check them using the
        <a class="link" href="functions.html#function_st-isvalid"><code class="literal">ST_IsValid()</code></a> function.
</p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="spatial-reference-systems"></a>11.5.5&nbsp;Spatial Reference System Support</h3>

</div>

</div>

</div>
<p>
        A spatial reference system (SRS) for spatial data is a
        coordinate-based system for geographic locations.
      </p><p>
        There are different types of spatial reference systems:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            A projected SRS is a projection of a globe onto a flat
            surface; that is, a flat map. For example, a light bulb
            inside a globe that shines on a paper cylinder surrounding
            the globe projects a map onto the paper. The result is
            georeferenced: Each point maps to a place on the globe. The
            coordinate system on that plane is Cartesian using a length
            unit (meters, feet, and so forth), rather than degrees of
            longitude and latitude.
          </p><p>
            The globes in this case are ellipsoids; that is, flattened
            spheres. Earth is a bit shorter in its North-South axis than
            its East-West axis, so a slightly flattened sphere is more
            correct, but perfect spheres permit faster calculations.
          </p></li><li class="listitem"><p>
            A geographic SRS is a nonprojected SRS representing
            longitude-latitude (or latitude-longitude) coordinates on an
            ellipsoid, in any angular unit.
          </p></li><li class="listitem"><p>
            The SRS denoted in MySQL by SRID 0 represents an infinite
            flat Cartesian plane with no units assigned to its axes.
            Unlike projected SRSs, it is not georeferenced and it does
            not necessarily represent Earth. It is an abstract plane
            that can be used for anything. SRID 0 is the default SRID
            for spatial data in MySQL.
</p></li></ul>
</div>
<p>
        MySQL maintains information about available spatial reference
        systems for spatial data in the data dictionary
        <code class="literal">mysql.st_spatial_reference_systems</code> table,
        which can store entries for projected and geographic SRSs. This
        data dictionary table is invisible, but SRS entry contents are
        available through the <code class="literal">INFORMATION_SCHEMA</code>
        <a class="link" href="information-schema.html#st-spatial-reference-systems-table" title="25.28 INFORMATION_SCHEMA ST_SPATIAL_REFERENCE_SYSTEMS表"><code class="literal">ST_SPATIAL_REFERENCE_SYSTEMS</code></a> table,
        implemented as a view on
        <code class="literal">mysql.st_spatial_reference_systems</code> (see
        <a class="xref" href="information-schema.html#st-spatial-reference-systems-table" title="25.28 INFORMATION_SCHEMA ST_SPATIAL_REFERENCE_SYSTEMS表">Section&nbsp;25.28, “The INFORMATION_SCHEMA ST_SPATIAL_REFERENCE_SYSTEMS Table”</a>).
      </p><p>
        The following example shows what an SRS entry looks like:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT *</code></strong>
       <strong class="userinput"><code>FROM INFORMATION_SCHEMA.ST_SPATIAL_REFERENCE_SYSTEMS</code></strong>
       <strong class="userinput"><code>WHERE SRS_ID = 4326\G</code></strong><font></font>
*************************** 1. row ***************************<font></font>
                SRS_NAME: WGS 84<font></font>
                  SRS_ID: 4326<font></font>
            ORGANIZATION: EPSG<font></font>
ORGANIZATION_COORDSYS_ID: 4326<font></font>
              DEFINITION: GEOGCS["WGS 84",DATUM["World Geodetic System 1984",<font></font>
                          SPHEROID["WGS 84",6378137,298.257223563,<font></font>
                          AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],<font></font>
                          PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],<font></font>
                          UNIT["degree",0.017453292519943278,<font></font>
                          AUTHORITY["EPSG","9122"]],<font></font>
                          AXIS["Lat",NORTH],AXIS["Long",EAST],<font></font>
                          AUTHORITY["EPSG","4326"]]<font></font>
             DESCRIPTION:<font></font>
</pre><p>
        This entry describes the SRS used for GPS systems. It has a name
        (<code class="literal">SRS_NAME</code>) of WGS 84 and an ID
        (<code class="literal">SRS_ID</code>) of 4326, which is the ID used by the
        <a class="ulink" href="http://epsg.org" target="_top">European Petroleum Survey
        Group</a> (EPSG).
      </p><p>
        SRS definitions in the <code class="literal">DEFINITION</code> column are
        WKT values, represented as specified in the
        <a class="ulink" href="http://www.opengeospatial.org" target="_top">Open Geospatial
        Consortium</a> document
        <a class="ulink" href="http://docs.opengeospatial.org/is/12-063r5/12-063r5.html" target="_top">OGC
        12-063r5</a>.
      </p><p>
        <code class="literal">SRS_ID</code> values represent the same kind of
        values passed as the SRID argument to spatial functions. SRID 0
        (the unitless Cartesian plane) is special. It is always a legal
        spatial reference system ID and can be used in any computations
        on spatial data that depend on SRID values.
      </p><p>
        For computations on multiple geometry values, all values must
        have the same SRID or an error occurs.
      </p><p>
        SRS definition parsing occurs on demand when definitions are
        needed by GIS functions. Parsed definitions are cached in the
        data dictionary cache so that parsing overhead is not incurred
        for every statement that needs SRS information.
      </p><p>
        To enable manipulation of SRS entries stored in the data
        dictionary, MySQL provides these SQL statements:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <a class="link" href="sql-syntax.html#create-spatial-reference-system" title="13.1.19创建空间参考系统语法"><code class="literal">CREATE SPATIAL REFERENCE
            SYSTEM</code></a>: See
            <a class="xref" href="sql-syntax.html#create-spatial-reference-system" title="13.1.19创建空间参考系统语法">Section&nbsp;13.1.19, “CREATE SPATIAL REFERENCE SYSTEM Syntax”</a>. The
            description for this statement includes additional
            information about SRS components.
          </p></li><li class="listitem"><p>
            <a class="link" href="sql-syntax.html#drop-spatial-reference-system" title="13.1.31 DROP空间参考系统语法"><code class="literal">DROP SPATIAL REFERENCE
            SYSTEM</code></a>: See
            <a class="xref" href="sql-syntax.html#drop-spatial-reference-system" title="13.1.31 DROP空间参考系统语法">Section&nbsp;13.1.31, “DROP SPATIAL REFERENCE SYSTEM Syntax”</a>.
</p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="creating-spatial-columns"></a>11.5.6&nbsp;Creating Spatial Columns</h3>

</div>

</div>

</div>
<p>
        MySQL provides a standard way of creating spatial columns for
        geometry types, for example, with <a class="link" href="sql-syntax.html#create-table" title="13.1.20 CREATE TABLE语法"><code class="literal">CREATE
        TABLE</code></a> or <a class="link" href="sql-syntax.html#alter-table" title="13.1.9 ALTER TABLE语法"><code class="literal">ALTER TABLE</code></a>.
        Spatial columns are supported for
        <a class="link" href="storage-engines.html#myisam-storage-engine" title="16.2 MyISAM存储引擎"><code class="literal">MyISAM</code></a>,
        <a class="link" href="innodb-storage-engine.html" title="第15章InnoDB存储引擎"><code class="literal">InnoDB</code></a>,
        <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a>, and
        <a class="link" href="storage-engines.html#archive-storage-engine" title="16.5 ARCHIVE存储引擎"><code class="literal">ARCHIVE</code></a> tables. See also the notes
        about spatial indexes under
        <a class="xref" href="data-types.html#creating-spatial-indexes" title="11.5.10创建空间索引">Section&nbsp;11.5.10, “Creating Spatial Indexes”</a>.
      </p><p>
        Columns with a spatial data type can have an SRID attribute, to
        explicitly indicate the spatial reference system (SRS) for
        values stored in the column. For implications of an
        SRID-restricted column, see
        <a class="xref" href="data-types.html#spatial-type-overview" title="11.5.1空间数据类型">Section&nbsp;11.5.1, “Spatial Data Types”</a>.
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Use the <a class="link" href="sql-syntax.html#create-table" title="13.1.20 CREATE TABLE语法"><code class="literal">CREATE TABLE</code></a>
            statement to create a table with a spatial column:
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE geom (g GEOMETRY);
</pre></li><li class="listitem"><p>
            Use the <a class="link" href="sql-syntax.html#alter-table" title="13.1.9 ALTER TABLE语法"><code class="literal">ALTER TABLE</code></a> statement
            to add or drop a spatial column to or from an existing
            table:
          </p><pre data-lang="sql" class="programlisting">ALTER TABLE geom ADD pt POINT;<font></font>
ALTER TABLE geom DROP pt;<font></font>
</pre></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="populating-spatial-columns"></a>11.5.7&nbsp;Populating Spatial Columns</h3>

</div>

</div>

</div>
<p>
        After you have created spatial columns, you can populate them
        with spatial data.
      </p><p>
        Values should be stored in internal geometry format, but you can
        convert them to that format from either Well-Known Text (WKT) or
        Well-Known Binary (WKB) format. The following examples
        demonstrate how to insert geometry values into a table by
        converting WKT values to internal geometry format:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Perform the conversion directly in the
            <a class="link" href="sql-syntax.html#insert" title="13.2.6 INSERT语法"><code class="literal">INSERT</code></a> statement:
          </p><pre data-lang="sql" class="programlisting">INSERT INTO geom VALUES (ST_GeomFromText('POINT(1 1)'));<font></font>
<font></font>
SET @g = 'POINT(1 1)';<font></font>
INSERT INTO geom VALUES (ST_GeomFromText(@g));<font></font>
</pre></li><li class="listitem"><p>
            Perform the conversion prior to the
            <a class="link" href="sql-syntax.html#insert" title="13.2.6 INSERT语法"><code class="literal">INSERT</code></a>:
          </p><pre data-lang="sql" class="programlisting">SET @g = ST_GeomFromText('POINT(1 1)');<font></font>
INSERT INTO geom VALUES (@g);<font></font>
</pre></li></ul>
</div>
<p>
        The following examples insert more complex geometries into the
        table:
      </p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">SET @g ='LINESTRING（0 0,1 1,2 2）';</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
INSERT INTO geom VALUES（ST_GeomFromText（@g））;</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
SET @g ='POLYGON（（0 0,10 0,10 10,0 10,0 0），（5 5,7 5,7 7,5 7,5 5））';</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
INSERT INTO geom VALUES（ST_GeomFromText（@g））;</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
SET @g =</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
'几何图形（点（1 1），LINESTRING（0 0,1 1,2 2,3 3,4 4））';</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
INSERT INTO geom VALUES（ST_GeomFromText（@g））;</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        前面的示例用于
         </font></font><a class="link" href="functions.html#function_st-geomfromtext"><code class="literal">ST_GeomFromText()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">创建几何值。</font><font style="vertical-align: inherit;">您还可以使用特定于类型的函数：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">SET @g ='POINT（1 1）';</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
INSERT INTO geom VALUES（ST_PointFromText（@g））;</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
SET @g ='LINESTRING（0 0,1 1,2 2）';</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
INSERT INTO geom VALUES（ST_LineStringFromText（@g））;</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
SET @g ='POLYGON（（0 0,10 0,10 10,0 10,0 0），（5 5,7 5,7 7,5 7,5 5））';</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
INSERT INTO geom VALUES（ST_PolygonFromText（@g））;</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
SET @g =</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
'几何图形（点（1 1），LINESTRING（0 0,1 1,2 2,3 3,4 4））';</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
INSERT INTO geom VALUES（ST_GeomCollFromText（@g））;</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        想要使用几何值的WKB表示的客户端应用程序负责将查询中正确形成的WKB发送到服务器。</font><font style="vertical-align: inherit;">有几种方法可以满足这一要求。</font><font style="vertical-align: inherit;">例如：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"></font><code class="literal">POINT(1 1)</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">使用十六进制文字语法</font><font style="vertical-align: inherit;">
            插入</font><font style="vertical-align: inherit;">值：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">插入geom VALUES</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
（ST_GeomFromWKB（X'0101000000000000000000F03F000000000000F03F'））;</font></font><font></font>
</pre></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            ODBC应用程序可以发送WKB表示，使用</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型</font><font style="vertical-align: inherit;">的参数将其绑定到占位符
             </font><font style="vertical-align: inherit;">：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">INSERT INTO geom VALUES（ST_GeomFromWKB（？））
</font></font></pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            其他编程接口可以支持类似的占位符机制。
          </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            在C程序中，您可以使用转义二进制值
             </font></font><a class="link" href="connectors-apis.html#mysql-real-escape-string-quote" title="28.7.7.56 mysql_real_escape_string_quote（）"><code class="literal">mysql_real_escape_string_quote()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            ，并将结果包含在发送到服务器的查询字符串中。</font><font style="vertical-align: inherit;">请参见
             </font></font><a class="xref" href="connectors-apis.html#mysql-real-escape-string-quote" title="28.7.7.56 mysql_real_escape_string_quote（）"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第28.7.7.56节“mysql_real_escape_string_quote（）”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="fetching-spatial-data"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.8获取空间数据</font></font></h3>

</div>

</div>

</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        存储在表中的几何值可以以内部格式获取。</font><font style="vertical-align: inherit;">您也可以将它们转换为WKT或WKB格式。
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            以内部格式获取空间数据：
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            使用内部格式获取几何值在表到表传输中非常有用：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">CREATE TABLE geom2（g GEOMETRY）SELECT g FROM geom;
</font></font></pre></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            以WKT格式获取空间数据：
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            该</font></font><a class="link" href="functions.html#function_st-astext"><code class="literal">ST_AsText()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">函数将几何从内部格式转换为WKT字符串。
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">SELECT ST_AsText（g）FROM geom;
</font></font></pre></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            以WKB格式获取空间数据：
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            该</font></font><a class="link" href="functions.html#function_st-asbinary"><code class="literal">ST_AsBinary()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">函数将几何从内部格式转换为</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">包含WKB值</font><font style="vertical-align: inherit;">的几何
             </font><font style="vertical-align: inherit;">。
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">SELECT ST_AsBinary（g）FROM geom;
</font></font></pre></li></ul>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="optimizing-spatial-analysis"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.9优化空间分析</font></font></h3>

</div>

</div>

</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        对于</font></font><a class="link" href="storage-engines.html#myisam-storage-engine" title="16.2 MyISAM存储引擎"><code class="literal">MyISAM</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><code class="literal">InnoDB</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表，可以使用</font></font><code class="literal">SPATIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">索引</font><font style="vertical-align: inherit;">优化包含空间数据的列中的搜索操作
         </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">最典型的操作是：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            点查询搜索包含给定点的所有对象
          </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            搜索与给定区域重叠的所有对象的区域查询
</font></font></p></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL使用</font></font><span class="bold"><strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">R-Trees</font></font></strong></span><font style="vertical-align: inherit;"></font><code class="literal">SPATIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">对空间列上</font><font style="vertical-align: inherit;">的</font><font style="vertical-align: inherit;">索引进行</font><span class="bold"><strong><font style="vertical-align: inherit;">二次分裂</font></strong></span><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">甲</font></font><code class="literal">SPATIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">指数使用几何的最小外接矩形（MBR）构建的。</font><font style="vertical-align: inherit;">对于大多数几何体，MBR是围绕几何形状的最小矩形。</font><font style="vertical-align: inherit;">对于水平或垂直线串，MBR是一个退化为线串的矩形。</font><font style="vertical-align: inherit;">对于某一点，MBR是一个退化为该点的矩形。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        也可以在空间列上创建普通索引。</font><font style="vertical-align: inherit;">在非</font></font><code class="literal">SPATIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">索引中，您必须为除列之外的任何空间列声明前缀
         </font></font><code class="literal">POINT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p>
        <code class="literal">MyISAM</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">并</font></font><code class="literal">InnoDB</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">支持</font></font><code class="literal">SPATIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和非</font></font><code class="literal">SPATIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">索引。</font><font style="vertical-align: inherit;">其他存储引擎支持非</font></font><code class="literal">SPATIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">索引，如
         </font></font><a class="xref" href="sql-syntax.html#create-index" title="13.1.15 CREATE INDEX语法"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第13.1.15节“CREATE INDEX语法”中所述</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="creating-spatial-indexes"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.10创建空间索引</font></font></h3>

</div>

</div>

</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        对于</font></font><code class="literal">InnoDB</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">MyISAM</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        表，MySQL可以使用类似于创建常规索引的语法创建空间索引，但使用
         </font></font><code class="literal">SPATIAL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">关键字。</font><font style="vertical-align: inherit;">必须声明空间索引中的列</font></font><code class="literal">NOT NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">以下示例演示了如何创建空间索引：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            用</font></font><a class="link" href="sql-syntax.html#create-table" title="13.1.20 CREATE TABLE语法"><code class="literal">CREATE TABLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">CREATE TABLE geom（g GEOMETRY NOT NULL SRID 4326，空间索引（g））;
</font></font></pre></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            用</font></font><a class="link" href="sql-syntax.html#alter-table" title="13.1.9 ALTER TABLE语法"><code class="literal">ALTER TABLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">CREATE TABLE geom（g GEOMETRY NOT NULL SRID 4326）;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
ALTER TABLE geom ADD SPATIAL INDEX（g）;</font></font><font></font>
</pre></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            用</font></font><a class="link" href="sql-syntax.html#create-index" title="13.1.15 CREATE INDEX语法"><code class="literal">CREATE INDEX</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">CREATE TABLE geom（g GEOMETRY NOT NULL SRID 4326）;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
创建空间索引g on geom（g）;</font></font><font></font>
</pre></li></ul>
</div>
<p>
        <code class="literal">SPATIAL INDEX</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">创建一个R树索引。</font><font style="vertical-align: inherit;">对于支持空间列的非空间索引的存储引擎，引擎会创建B树索引。</font><font style="vertical-align: inherit;">空间值的B树索引对精确值查找很有用，但对于范围扫描则不行。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        优化程序可以使用在SRID限制的列上定义的空间索引。</font><font style="vertical-align: inherit;">有关更多信息，请参见
         </font></font><a class="xref" href="data-types.html#spatial-type-overview" title="11.5.1空间数据类型"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.5.1节“空间数据类型”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="xref" href="optimization.html#spatial-index-optimization" title="8.3.3空间索引优化"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第8.3.3节“空间索引优化”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        有关索引空间列的更多信息，请参见
         </font></font><a class="xref" href="sql-syntax.html#create-index" title="13.1.15 CREATE INDEX语法"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第13.1.15节“CREATE INDEX语法”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        要删除空间索引，请使用</font></font><a class="link" href="sql-syntax.html#alter-table" title="13.1.9 ALTER TABLE语法"><code class="literal">ALTER
        TABLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或</font></font><a class="link" href="sql-syntax.html#drop-index" title="13.1.27 DROP INDEX语法"><code class="literal">DROP INDEX</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            用</font></font><a class="link" href="sql-syntax.html#alter-table" title="13.1.9 ALTER TABLE语法"><code class="literal">ALTER TABLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">ALTER TABLE geom DROP INDEX g;
</font></font></pre></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            用</font></font><a class="link" href="sql-syntax.html#drop-index" title="13.1.27 DROP INDEX语法"><code class="literal">DROP INDEX</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">DROP INDEX g ON geom;
</font></font></pre></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        示例：假设一个表</font></font><code class="literal">geom</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">包含超过32,000个几何，这些几何存储在</font></font><code class="literal">g</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型</font><font style="vertical-align: inherit;">列
         </font><font style="vertical-align: inherit;">中</font></font><code class="literal">GEOMETRY</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">该表还有一个</font><font style="vertical-align: inherit;">用于存储对象ID值</font><font style="vertical-align: inherit;">的</font></font><code class="literal">AUTO_INCREMENT</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列
         </font></font><code class="literal">fid</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>DESCRIBE geom;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------- + ---------- + ------ + ------ + --------- + ------- --------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">领域| </font><font style="vertical-align: inherit;">输入| </font><font style="vertical-align: inherit;">空| </font><font style="vertical-align: inherit;">钥匙| </font><font style="vertical-align: inherit;">默认| </font><font style="vertical-align: inherit;">额外的|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------- + ---------- + ------ + ------ + --------- + ------- --------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">fid | </font><font style="vertical-align: inherit;">int（11）| </font><font style="vertical-align: inherit;">| </font><font style="vertical-align: inherit;">PRI | </font><font style="vertical-align: inherit;">NULL | </font><font style="vertical-align: inherit;">auto_increment |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">g | </font><font style="vertical-align: inherit;">几何| </font><font style="vertical-align: inherit;">| </font><font style="vertical-align: inherit;">| </font><font style="vertical-align: inherit;">| </font><font style="vertical-align: inherit;">|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------- + ---------- + ------ + ------ + --------- + ------- --------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
2行（0.00秒）</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT COUNT(*) FROM geom;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">count（*）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">32376 |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
1排（0.00秒）</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        要在列上添加空间索引</font></font><code class="literal">g</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，请使用以下语句：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>ALTER TABLE geom ADD SPATIAL INDEX(g);</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
查询OK，受影响的32376行（4.05秒）</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
记录：32376重复：0警告：0</font></font><font></font>
</pre>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a name="using-spatial-indexes"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.5.11使用空间索引</font></font></h3>

</div>

</div>

</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        优化程序调查可用空间索引是否可以参与搜索使用诸如</font></font><a class="link" href="functions.html#function_mbrcontains"><code class="literal">MBRContains()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
         </font></font><a class="link" href="functions.html#function_mbrwithin"><code class="literal">MBRWithin()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在
         </font></font><code class="literal">WHERE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">子句中</font><font style="vertical-align: inherit;">的函数的查询</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">以下查询查找给定矩形中的所有对象：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql&gt; </font></font><strong class="userinput"><code>SET @poly =</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    - &gt; </font><font style="vertical-align: inherit;">
mysql&gt; </font><font style="vertical-align: inherit;">
    - &gt;</font></font><strong class="userinput"><code>'Polygon((30000 15000,<font></font>
                 31000 15000,<font></font>
                 31000 16000,<font></font>
                 30000 16000,<font></font>
                 30000 15000))';</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>SELECT fid,ST_AsText(g) FROM geom WHERE</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>MBRContains(ST_GeomFromText(@poly),g);</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----- + ------------------------------------------- -------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">fid | </font><font style="vertical-align: inherit;">ST_AsText（g）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----- + ------------------------------------------- -------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">21 | </font><font style="vertical-align: inherit;">LINESTRING（30350.4 15828.8,30350.6 15845,30333.8 15845,30 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">22 | </font><font style="vertical-align: inherit;">LINESTRING（30350.6 15871.4,30350.6 15887.8,30334 15887.8，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">23 | </font><font style="vertical-align: inherit;">LINESTRING（30350.6 15914.2,30350.6 15930.4,30334 15930.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">24 | </font><font style="vertical-align: inherit;">LINESTRING（30290.2 15823,30290.2 15839.4,30273.4 15839.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">25 | </font><font style="vertical-align: inherit;">LINESTRING（30291.4 15866.2,30291.6 15882.4,30274.8 15882. ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">26 | </font><font style="vertical-align: inherit;">LINESTRING（30291.6 15918.2,30291.6 15934.4,30275 15934.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">249 | </font><font style="vertical-align: inherit;">LINESTRING（30337.8 15938.6,30337.8 15946.8,30320.4 15946. ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">1 | </font><font style="vertical-align: inherit;">LINESTRING（30250.4 15129.2,30248.8 15138.4,30238.2 15136. ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">2 | </font><font style="vertical-align: inherit;">LINESTRING（30220.2 15122.8,30217.2 15137.8,30207.6 15136，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">3 | </font><font style="vertical-align: inherit;">LINESTRING（30179 15114.4,30176.6 15129.4,30167 15128,3016 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">4 | </font><font style="vertical-align: inherit;">LINESTRING（30155.2 15121.4,30140.4 15118.6,30142 15109,30 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">5 | </font><font style="vertical-align: inherit;">LINESTRING（30192.4 15085,30177.6 15082.2,30179.2 15072.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">6 | </font><font style="vertical-align: inherit;">LINESTRING（30244 15087,30229 15086.2,30229.4 15076.4,3024 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">7 | </font><font style="vertical-align: inherit;">LINESTRING（30200.6 15059.4,30185.6 15058.6,30186 15048.8，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">10 | </font><font style="vertical-align: inherit;">LINESTRING（30179.6 15017.8,30181 15002.8,30190.8 15003.6，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">11 | </font><font style="vertical-align: inherit;">LINESTRING（30154.2 15000.4,30168.6 15004.8,30166 15014.2，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">13 | </font><font style="vertical-align: inherit;">LINESTRING（30105 15065.8,30108.4 15050.8,30118 15053,3011 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">154 | </font><font style="vertical-align: inherit;">LINESTRING（30276.2 15143.8,30261.4 15141,30263 15131.4,30 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">155 | </font><font style="vertical-align: inherit;">LINESTRING（30269.8 15084,30269.4 15093.4,30258.6 15093,30 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">157 | </font><font style="vertical-align: inherit;">LINESTRING（30128.2 15011,30113.2 15010.2,30113.6 15000.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----- + ------------------------------------------- -------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
20行（0.00秒）</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        使用</font></font><a class="link" href="sql-syntax.html#explain" title="13.8.2 EXPLAIN语法"><code class="literal">EXPLAIN</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">检查执行该查询方式：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql&gt; </font></font><strong class="userinput"><code>SET @poly =</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    - &gt; </font><font style="vertical-align: inherit;">
mysql&gt; </font><font style="vertical-align: inherit;">
    - &gt;</font></font><strong class="userinput"><code>'Polygon((30000 15000,<font></font>
                 31000 15000,<font></font>
                 31000 16000,<font></font>
                 30000 16000,<font></font>
                 30000 15000))';</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>EXPLAIN SELECT fid,ST_AsText(g) FROM geom WHERE</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>MBRContains(ST_GeomFromText(@poly),g)\G</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
*************************** 1。排******************** *******</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
           id：1</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  select_type：SIMPLE</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        表：geom</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
         类型：范围</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
possible_keys：g</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          关键：g</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      key_len：32</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          ref：NULL</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
         行：50</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        额外：使用在哪里</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
1排（0.00秒）</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        检查没有空间索引会发生什么：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql&gt; </font></font><strong class="userinput"><code>SET @poly =</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    - &gt; </font><font style="vertical-align: inherit;">
mysql&gt; </font><font style="vertical-align: inherit;">
    - &gt;</font></font><strong class="userinput"><code>'Polygon((30000 15000,<font></font>
                 31000 15000,<font></font>
                 31000 16000,<font></font>
                 30000 16000,<font></font>
                 30000 15000))';</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>EXPLAIN SELECT fid,ST_AsText(g) FROM g IGNORE INDEX (g) WHERE</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>MBRContains(ST_GeomFromText(@poly),g)\G</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
*************************** 1。排******************** *******</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
           id：1</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
  select_type：SIMPLE</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        表：geom</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
         类型：全部</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
possible_keys：NULL</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          key：NULL</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      key_len：NULL</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          ref：NULL</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
         行：32376</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        额外：使用在哪里</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
1排（0.00秒）</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在</font></font><a class="link" href="sql-syntax.html#select" title="13.2.10 SELECT语法"><code class="literal">SELECT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">没有空间索引的情况下</font><font style="vertical-align: inherit;">执行</font><font style="vertical-align: inherit;">语句会产生相同的结果，但会导致执行时间从0.00秒增加到0.46秒：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql&gt; </font></font><strong class="userinput"><code>SET @poly =</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    - &gt; </font><font style="vertical-align: inherit;">
mysql&gt; </font><font style="vertical-align: inherit;">
    - &gt;</font></font><strong class="userinput"><code>'Polygon((30000 15000,<font></font>
                 31000 15000,<font></font>
                 31000 16000,<font></font>
                 30000 16000,<font></font>
                 30000 15000))';</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>SELECT fid,ST_AsText(g) FROM geom IGNORE INDEX (g) WHERE</code></strong><font style="vertical-align: inherit;"></font><strong class="userinput"><code>MBRContains(ST_GeomFromText(@poly),g);</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----- + ------------------------------------------- -------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">fid | </font><font style="vertical-align: inherit;">ST_AsText（g）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----- + ------------------------------------------- -------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">1 | </font><font style="vertical-align: inherit;">LINESTRING（30250.4 15129.2,30248.8 15138.4,30238.2 15136. ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">2 | </font><font style="vertical-align: inherit;">LINESTRING（30220.2 15122.8,30217.2 15137.8,30207.6 15136，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">3 | </font><font style="vertical-align: inherit;">LINESTRING（30179 15114.4,30176.6 15129.4,30167 15128,3016 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">4 | </font><font style="vertical-align: inherit;">LINESTRING（30155.2 15121.4,30140.4 15118.6,30142 15109,30 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">5 | </font><font style="vertical-align: inherit;">LINESTRING（30192.4 15085,30177.6 15082.2,30179.2 15072.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">6 | </font><font style="vertical-align: inherit;">LINESTRING（30244 15087,30229 15086.2,30229.4 15076.4,3024 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">7 | </font><font style="vertical-align: inherit;">LINESTRING（30200.6 15059.4,30185.6 15058.6,30186 15048.8，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">10 | </font><font style="vertical-align: inherit;">LINESTRING（30179.6 15017.8,30181 15002.8,30190.8 15003.6，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">11 | </font><font style="vertical-align: inherit;">LINESTRING（30154.2 15000.4,30168.6 15004.8,30166 15014.2，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">13 | </font><font style="vertical-align: inherit;">LINESTRING（30105 15065.8,30108.4 15050.8,30118 15053,3011 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">21 | </font><font style="vertical-align: inherit;">LINESTRING（30350.4 15828.8,30350.6 15845,30333.8 15845,30 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">22 | </font><font style="vertical-align: inherit;">LINESTRING（30350.6 15871.4,30350.6 15887.8,30334 15887.8，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">23 | </font><font style="vertical-align: inherit;">LINESTRING（30350.6 15914.2,30350.6 15930.4,30334 15930.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">24 | </font><font style="vertical-align: inherit;">LINESTRING（30290.2 15823,30290.2 15839.4,30273.4 15839.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">25 | </font><font style="vertical-align: inherit;">LINESTRING（30291.4 15866.2,30291.6 15882.4,30274.8 15882. ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">26 | </font><font style="vertical-align: inherit;">LINESTRING（30291.6 15918.2,30291.6 15934.4,30275 15934.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">154 | </font><font style="vertical-align: inherit;">LINESTRING（30276.2 15143.8,30261.4 15141,30263 15131.4,30 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">155 | </font><font style="vertical-align: inherit;">LINESTRING（30269.8 15084,30269.4 15093.4,30258.6 15093,30 ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">157 | </font><font style="vertical-align: inherit;">LINESTRING（30128.2 15011,30113.2 15010.2,30113.6 15000.4，... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">249 | </font><font style="vertical-align: inherit;">LINESTRING（30337.8 15938.6,30337.8 15946.8,30320.4 15946. ... |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----- + ------------------------------------------- -------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
20行（0.46秒）</font></font><font></font>
</pre>
</div>

</div>

<div class="section">

<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="json"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.6 JSON数据类型</font></font></h2>

</div>

</div>

</div>
<a class="indexterm" name="idm140091691887184"></a>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><a class="xref" href="data-types.html#json-values" title="创建JSON值"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">创建JSON值</font></font></a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#json-normalization" title="JSON值的规范化，合并和自动包装"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON值的规范化，合并和自动包装</font></font></a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#json-paths" title="搜索和修改JSON值"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">搜索和修改JSON值</font></font></a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#json-path-syntax" title="JSON路径语法"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON路径语法</font></font></a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#json-comparison" title="JSON值的比较和排序"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON值的比较和排序</font></font></a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#json-converting-between-types" title="在JSON和非JSON值之间转换"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在JSON和非JSON值之间转换</font></font></a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#json-aggregation" title="JSON值的聚合"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON值的聚合</font></font></a></p></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      MySQL支持</font><a class="ulink" href="https://tools.ietf.org/html/rfc7159" target="_top"><font style="vertical-align: inherit;">RFC 7159</font></a></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">定义</font><font style="vertical-align: inherit;">的本机</font><font style="vertical-align: inherit;">数据类型</font><font style="vertical-align: inherit;">，可以高效访问JSON（JavaScript Object Notation）文档中的数据。</font><font style="vertical-align: inherit;">与
       </font><font style="vertical-align: inherit;">在字符串列中存储JSON格式字符串相比，数据类型具有以下优势：
</font></font><a class="ulink" href="https://tools.ietf.org/html/rfc7159" target="_top"><font style="vertical-align: inherit;"></font></a><font style="vertical-align: inherit;"></font><code class="literal">JSON</code><font style="vertical-align: inherit;"></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          存储在</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列中</font><font style="vertical-align: inherit;">的JSON文档的自动验证
           </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">无效的文档会产生错误。
        </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          优化的存储格式。</font><font style="vertical-align: inherit;">存储在</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列中的</font><font style="vertical-align: inherit;">JSON文档将
           </font><font style="vertical-align: inherit;">转换为内部格式，以允许对文档元素进行快速读取访问。</font><font style="vertical-align: inherit;">当服务器稍后必须读取以该二进制格式存储的JSON值时，不需要从文本表示中解析该值。</font><font style="vertical-align: inherit;">二进制格式的结构使服务器能够直接通过键或数组索引查找子对象或嵌套值，而无需在文档之前或之后读取所有值。
</font></font></p></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      MySQL 8.0还支持</font><font style="vertical-align: inherit;">使用该</font><font style="vertical-align: inherit;">
      函数</font><font style="vertical-align: inherit;">在</font><a class="ulink" href="https://tools.ietf.org/html/rfc7396" target="_top"><font style="vertical-align: inherit;">RFC 7396中</font></a><font style="vertical-align: inherit;">定义</font><font style="vertical-align: inherit;">的</font></font><span class="emphasis"><em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON Merge Patch</font></font></em></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">格式
       </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">有关</font><font style="vertical-align: inherit;">示例和更多信息，</font><font style="vertical-align: inherit;">请参阅此函数的说明以及
       </font><a class="xref" href="data-types.html#json-normalization" title="JSON值的规范化，合并和自动包装"><font style="vertical-align: inherit;">JSON值的规范化，合并和自动包装</font></a><font style="vertical-align: inherit;">。
</font></font><a class="ulink" href="https://tools.ietf.org/html/rfc7396" target="_top"><font style="vertical-align: inherit;"></font></a><font style="vertical-align: inherit;"></font><a class="link" href="functions.html#function_json-merge-patch"><code class="literal">JSON_MERGE_PATCH()</code></a><font style="vertical-align: inherit;"></font><a class="xref" href="data-types.html#json-normalization" title="JSON值的规范化，合并和自动包装"><font style="vertical-align: inherit;"></font></a><font style="vertical-align: inherit;"></font></p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
注意
</font></font></div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        此讨论使用</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">monotype来指定JSON数据类型和</font><font style="vertical-align: inherit;">
        常规字体中的</font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">“ </font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> ”</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以指示一般的JSON数据。
</font></font></p>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      存储</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">文档</font><font style="vertical-align: inherit;">所需的空间</font><font style="vertical-align: inherit;">与</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGBLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
       </font><font style="vertical-align: inherit;">大致相同</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGTEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">; </font><font style="vertical-align: inherit;">有关更多信息</font></font><a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">请参见
       </font><a class="xref" href="data-types.html#storage-requirements" title="11.8数据类型存储要求"><font style="vertical-align: inherit;">第11.8节“数据类型存储要求”</font></a><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">请务必记住，</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列中</font><font style="vertical-align: inherit;">存储的任何JSON文档的大小</font><font style="vertical-align: inherit;">都限制为</font></font><a class="link" href="server-administration.html#sysvar_max_allowed_packet"><code class="literal">max_allowed_packet</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">系统变量</font><font style="vertical-align: inherit;">的值</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">（当服务器在内存中内部操作JSON值时，它可能大于此值;当服务器存储时，该限制适用。）您可以使用该</font></font><a class="link" href="functions.html#function_json-storage-size"><code class="literal">JSON_STORAGE_SIZE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">函数</font><font style="vertical-align: inherit;">获取存储JSON文档所需的空间量
       </font><font style="vertical-align: inherit;">; </font><font style="vertical-align: inherit;">请注意</font></font><a class="link" href="data-types.html#json" title="11.6 JSON数据类型"><code class="literal">JSON</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> 列，存储大小 - 以及此函数返回的值 - 是在可能已对其执行的任何部分更新之前由列使用的值（请参阅本节后面的JSON部分更新优化的讨论）。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      在MySQL 8.0.13之前，</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列不能具有非</font></font><code class="literal">NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">默认值。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      除了</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型之外，还有一组SQL函数可用于对JSON值进行操作，例如创建，操作和搜索。</font><font style="vertical-align: inherit;">以下讨论显示了这些操作的示例。</font><font style="vertical-align: inherit;">有关各个函数的详细信息，请参见</font></font><a class="xref" href="functions.html#json-functions" title="12.17 JSON函数"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第12.17节“JSON函数”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      还提供了一组用于操作GeoJSON值的空间函数。</font><font style="vertical-align: inherit;">请参见</font></font><a class="xref" href="functions.html#spatial-geojson-functions" title="12.16.11空间GeoJSON函数"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第12.16.11节“空间GeoJSON函数”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
    </font></font></p><p>
      <code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列，如其他二进制类型的列，不直接索引; </font><font style="vertical-align: inherit;">相反，您可以在生成的列上创建索引，该列从列中提取标量值
       </font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">有关</font><font style="vertical-align: inherit;">详细示例，</font><font style="vertical-align: inherit;">请参阅
       </font></font><a class="xref" href="sql-syntax.html#json-column-indirect-index" title="索引生成的列以提供JSON列索引"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">索引生成的列以提供JSON列索引</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      MySQL优化器还在与JSON表达式匹配的虚拟列上查找兼容索引。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      MySQL NDB Cluster 8.0支持</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列和MySQL JSON函数，包括在列生成的</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列</font><font style="vertical-align: inherit;">上创建索引</font><font style="vertical-align: inherit;">，作为无法索引</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列</font><font style="vertical-align: inherit;">的变通方法</font><font style="vertical-align: inherit;">。</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">每个</font></font><a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表</font><font style="vertical-align: inherit;">最多</font><font style="vertical-align: inherit;">支持</font><font style="vertical-align: inherit;">3 </font><font style="vertical-align: inherit;">列
       </font><font style="vertical-align: inherit;">。
</font></font></p>
<h3><a name="json-partial-updates"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON值的部分更新</font></font></h3>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      在MySQL 8.0中，优化器可以执行列的部分就地更新，</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">而不是删除旧文档并将新文档完整地写入列。</font><font style="vertical-align: inherit;">可以对满足以下条件的更新执行此优化：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          正在更新的列被声明为
           </font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
        </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          该</font></font><a class="link" href="sql-syntax.html#update" title="13.2.12 UPDATE语法"><code class="literal">UPDATE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">语句使用任何的三个功能
           </font></font><a class="link" href="functions.html#function_json-set"><code class="literal">JSON_SET()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
           </font></font><a class="link" href="functions.html#function_json-replace"><code class="literal">JSON_REPLACE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
           </font></font><a class="link" href="functions.html#function_json-remove"><code class="literal">JSON_REMOVE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">更新列。</font></font><code class="literal">UPDATE mytable SET jcol = '{"a": 10, "b":
          25}'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">无法</font><font style="vertical-align: inherit;">直接指定列值（例如
           </font><font style="vertical-align: inherit;">）作为部分更新。
        </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">可以以这种方式优化</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">单个</font></font><code class="literal">UPDATE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">语句</font><font style="vertical-align: inherit;">
          中多</font><font style="vertical-align: inherit;">列的</font><font style="vertical-align: inherit;">更新</font><font style="vertical-align: inherit;">; </font><font style="vertical-align: inherit;">MySQL只能对使用刚才列出的三个函数更新其值的列执行部分更新。
        </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          输入列和目标列必须是同一列; </font><font style="vertical-align: inherit;">诸如</font></font><code class="literal">UPDATE mytable SET jcol1
          = JSON_SET(jcol2, '$.a', 100)</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">不能作为部分更新执行</font><font style="vertical-align: inherit;">的语句</font><font style="vertical-align: inherit;">。
        </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          只要输入列和目标列相同，更新就可以以任意组合使用对前一项中列出的任何函数的嵌套调用。
        </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          所有更改都使用新值替换现有数组或对象值，并且不向父对象或数组添加任何新元素。
        </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          被替换的值必须至少与替换值一样大。</font><font style="vertical-align: inherit;">换句话说，新值不能大于旧值。
        </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          当前一次部分更新为较大值留出足够空间时，会出现此要求的可能例外情况。</font><font style="vertical-align: inherit;">您可以使用该函数
           </font></font><a class="link" href="functions.html#function_json-storage-free"><code class="literal">JSON_STORAGE_FREE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">查看</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列的</font><font style="vertical-align: inherit;">任何部分更新释放了多少空间
           </font><font style="vertical-align: inherit;">。
</font></font></p></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      可以使用节省空间的紧凑格式将这种部分更新写入二进制日志; </font><font style="vertical-align: inherit;">这可以通过将</font></font><a class="link" href="replication.html#sysvar_binlog_row_value_options"><code class="literal">binlog_row_value_options</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      系统变量</font><font style="vertical-align: inherit;">设置为来启用</font></font><code class="literal">PARTIAL_JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">有关更多信息，请参阅此变量的说明。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      接下来的几节提供了有关JSON值的创建和操作的基本信息。
</font></font></p>
<div class="simplesect">

<div class="titlepage">
<div>

<div class="simple">
<h3 class="title"><a name="json-values"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">创建JSON值</font></font></h3>
</div>
</div>
</div>
<a class="indexterm" name="idm140091691818816"></a><a class="indexterm" name="idm140091691817328"></a><a class="indexterm" name="idm140091691815840"></a><a class="indexterm" name="idm140091691814352"></a><a class="indexterm" name="idm140091691812864"></a><a class="indexterm" name="idm140091691811376"></a><a class="indexterm" name="idm140091691809888"></a><a class="indexterm" name="idm140091691808400"></a><a class="indexterm" name="idm140091691806912"></a><a class="indexterm" name="idm140091691805424"></a><a class="indexterm" name="idm140091691803936"></a><a class="indexterm" name="idm140091691802448"></a><a class="indexterm" name="idm140091691800960"></a><a class="indexterm" name="idm140091691799472"></a><a class="indexterm" name="idm140091691797984"></a><a class="indexterm" name="idm140091691796496"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        JSON数组包含一个由逗号分隔的值列表，并包含在</font><font style="vertical-align: inherit;">
        字符</font></font><code class="literal">[</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">]</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符中：
      </font></font></p><pre data-lang="json" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">[“abc”，10，null，true，false]
</font></font></pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        JSON对象包含一组由逗号分隔的键值对，并包含在</font><font style="vertical-align: inherit;">字符</font></font><code class="literal">{</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><code class="literal">}</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符中：
      </font></font></p><pre data-lang="json" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">{“k1”：“value”，“k2”：10}
</font></font></pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如示例所示，JSON数组和对象可以包含字符串或数字的标量值，JSON空文字或JSON布尔值true或false文字。</font><font style="vertical-align: inherit;">JSON对象中的键必须是字符串。</font><font style="vertical-align: inherit;">时间（日期，时间或日期时间）标量值也是允许的：
      </font></font></p><pre data-lang="json" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">[“12：18：29.000000”，“2015-07-29”，“2015-07-29 12：18：29.000000”]
</font></font></pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在JSON数组元素和JSON对象键值中允许嵌套：
      </font></font></p><pre data-lang="json" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">[99，{“id”：“HK500”，“费用”：75.99}，[“热”，“冷”]]</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
{“k1”：“value”，“k2”：[10,20]}</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        您还可以为此目的从MySQL提供的许多函数中获取JSON值（请参见
         </font></font><a class="xref" href="functions.html#json-creation-functions" title="12.17.2创建JSON值的函数"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第12.17.2节“创建JSON值的函数”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）以及</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">使用</font><font style="vertical-align: inherit;">其他类型的值</font><a class="xref" href="data-types.html#json-converting-between-types" title="在JSON和非JSON值之间转换"><font style="vertical-align: inherit;">转换</font></a><font style="vertical-align: inherit;">为</font><font style="vertical-align: inherit;">类型
         </font><font style="vertical-align: inherit;">（请参阅
         </font><a class="xref" href="data-types.html#json-converting-between-types" title="在JSON和非JSON值之间转换"><font style="vertical-align: inherit;">在JSON之间转换）和非JSON值</font></a><font style="vertical-align: inherit;">）。</font><font style="vertical-align: inherit;">接下来的几段描述了MySQL如何处理作为输入提供的JSON值。
      </font></font><a class="link" href="functions.html#function_cast"><code class="literal">CAST(<em class="replaceable"><code>value</code></em> AS
        JSON)</code></a><font style="vertical-align: inherit;"></font><a class="xref" href="data-types.html#json-converting-between-types" title="在JSON和非JSON值之间转换"><font style="vertical-align: inherit;"></font></a><font style="vertical-align: inherit;"></font></p><a class="indexterm" name="idm140091691781344"></a><a class="indexterm" name="idm140091691780272"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在MySQL中，JSON值被写为字符串。</font><font style="vertical-align: inherit;">MySQL解析在需要JSON值的上下文中使用的任何字符串，如果它作为JSON无效则会产生错误。</font><font style="vertical-align: inherit;">这些上下文包括将值插入到具有</font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型</font><font style="vertical-align: inherit;">的列中，
         </font><font style="vertical-align: inherit;">并将参数传递给期望JSON值的函数（通常显示为
         </font></font><em class="replaceable"><code>json_doc</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
         </font></font><em class="replaceable"><code>json_val</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在MySQL JSON函数的文档中），如以下示例所示：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            如果值是有效的JSON值，则</font><font style="vertical-align: inherit;">
            尝试将值插入</font><font style="vertical-align: inherit;">列成功，但如果不是，则</font><font style="vertical-align: inherit;">尝试</font><font style="vertical-align: inherit;">失败：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>CREATE TABLE t1 (jdoc JSON);</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
查询OK，0行受影响（0.20秒）</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>INSERT INTO t1 VALUES('{"key1": "value1", "key2": "value2"}');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
查询OK，1行受影响（0.01秒）</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>INSERT INTO t1 VALUES('[1, 2,');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
ERROR 3140（22032）第2行：无效的JSON文本：</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
“无效值。” </font><font style="vertical-align: inherit;">在位置6的值（或列）'[1,2，'。</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            为位置</font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">“ </font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在位置
            </font></font><em class="replaceable"><code>N</code></em></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> ”</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在这样的错误消息是基于0的，但应考虑其中一个值问题实际发生的粗指示。
          </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            该</font></font><a class="link" href="functions.html#function_json-type"><code class="literal">JSON_TYPE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">函数需要一个JSON参数并尝试将其解析为JSON值。</font><font style="vertical-align: inherit;">如果值有效，则返回值的JSON类型，否则产生错误：
          </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_TYPE('["a", "b", 1]');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">JSON_TYPE（'[“a”，“b”，1]'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">ARRAY |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------------- +</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_TYPE('"hello"');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">JSON_TYPE（'“hello”'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">STRING |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------- +</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_TYPE('hello');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
ERROR 3146（22032）：参数1中JSON数据的数据类型无效</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
使用json_type; </font><font style="vertical-align: inherit;">需要JSON字符串或JSON类型。</font></font><font></font>
</pre></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL使用</font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符集和</font></font><code class="literal">utf8mb4_bin</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">排序规则</font><font style="vertical-align: inherit;">处理JSON上下文中使用的
         </font><font style="vertical-align: inherit;">字符串
         </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">其他字符集中的字符串将</font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">根据需要</font><font style="vertical-align: inherit;">进行转换</font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">（对于在串</font></font><code class="literal">ascii</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
         </font></font><code class="literal">utf8</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符集，则不需要转换，因为</font></font><code class="literal">ascii</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">utf8</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">都是的子集</font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。）
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        作为使用文字字符串编写JSON值的替代方法，存在用于从组件元素组成JSON值的函数。</font></font><a class="link" href="functions.html#function_json-array"><code class="literal">JSON_ARRAY()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">获取（可能为空）值列表并返回包含这些值的JSON数组：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_ARRAY('a', 1, NOW());</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">JSON_ARRAY（'a'，1，NOW（））|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">[“a”，1，“2015-07-27 09：43：47.000000”] |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------------------------- +</font></font><font></font>
</pre><p>
        <a class="link" href="functions.html#function_json-object"><code class="literal">JSON_OBJECT()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> 获取（可能为空）键值对列表并返回包含这些对的JSON对象：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_OBJECT('key1', 1, 'key2', 'abc');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ --------------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">JSON_OBJECT（'​​key1'，1，'key2'，'abc'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ --------------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">{“key1”：1，“key2”：“abc”} |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ --------------------------------------- +</font></font><font></font>
</pre><p>
        <a class="link" href="functions.html#function_json-merge-preserve"><code class="literal">JSON_MERGE_PRESERVE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> 获取两个或多个JSON文档并返回组合结果：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_MERGE_PRESERVE('["a", 1]', '{"key": "value"}');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- ---- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">JSON_MERGE_PRESERVE（'[“a”，1]'，'{“key”：“value”}'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- ---- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">[“a”，1，{“key”：“value”}] |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- ---- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
1排（0.00秒）</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        有关合并规则的信息，请参阅
         </font></font><a class="xref" href="data-types.html#json-normalization" title="JSON值的规范化，合并和自动包装"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON值的规范化，合并和自动包装</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        （MySQL 8.0.3及更高版本也支持
         </font></font><a class="link" href="functions.html#function_json-merge-patch"><code class="literal">JSON_MERGE_PATCH()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，它有一些不同的行为。有关</font><font style="vertical-align: inherit;">这两个函数之间的差异的信息，</font><font style="vertical-align: inherit;">请参阅与
         </font></font><a class="xref" href="functions.html#json-merge-patch-json-merge-preserve-compared" title="JSON_MERGE_PATCH（）与JSON_MERGE_PRESERVE（）进行比较"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON_MERGE_PRESERVE（）相比较的JSON_MERGE_PATCH（）</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。）
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        可以将JSON值分配给用户定义的变量：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql&gt; </font></font><strong class="userinput"><code>SET @j = JSON_OBJECT('key', 'value');</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
mysql&gt;</font></font><strong class="userinput"><code>SELECT @j;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">@j |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">{“key”：“value”} |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------ +</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        但是，用户定义的变量不能是
         </font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型，所以虽然
         </font></font><code class="literal">@j</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在前面的例子中看起来像一个JSON值并且具有相同的字符集和归类为JSON值，但它</font></font><span class="emphasis"><em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">不</font></font></em></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">具有
         </font></font><code class="literal">JSON</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型。</font><font style="vertical-align: inherit;">相反，结果from
         </font></font><a class="link" href="functions.html#function_json-object"><code class="literal">JSON_OBJECT()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在分配给变量时会转换为字符串。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        通过转换JSON值生成的字符串具有以下字符集</font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和排序规则
         </font></font><code class="literal">utf8mb4_bin</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT CHARSET(@j), COLLATION(@j);</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------- + --------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">CHARSET（@j）| </font><font style="vertical-align: inherit;">COLLATION（@j）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------- + --------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">utf8mb4 | </font><font style="vertical-align: inherit;">utf8mb4_bin |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------- + --------------- +</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        因为</font></font><code class="literal">utf8mb4_bin</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是二进制排序规则，所以JSON值的比较区分大小写。
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_ARRAY('x') = JSON_ARRAY('X');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">JSON_ARRAY（'x'）= JSON_ARRAY（'X'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">0 |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----------------------------------- +</font></font><font></font>
</pre><a class="indexterm" name="idm140091691727632"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        区分大小写也适用于JSON
         </font></font><code class="literal">null</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，</font></font><code class="literal">true</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><code class="literal">false</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">文字，它们必须始终以小写形式编写：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_VALID('null'), JSON_VALID('Null'), JSON_VALID('NULL');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------------- + -------------------- + ------- ------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">JSON_VALID（'null'）| </font><font style="vertical-align: inherit;">JSON_VALID（'Null'）| </font><font style="vertical-align: inherit;">JSON_VALID（'NULL'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------------- + -------------------- + ------- ------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">1 | </font><font style="vertical-align: inherit;">0 | </font><font style="vertical-align: inherit;">0 |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------------- + -------------------- + ------- ------------- +</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT CAST('null' AS JSON);</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">CAST（'null'AS JSON）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">null |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ---------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
1排（0.00秒）</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT CAST('NULL' AS JSON);</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
ERROR 3141（22032）：参数1中的函数cast_as_json中的JSON文本无效：</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
“无效值。” </font><font style="vertical-align: inherit;">位于'NULL'的0位置。</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        的字面JSON的情况下，灵敏度不同于所述SQL的</font></font><code class="literal">NULL</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，</font></font><code class="literal">TRUE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><code class="literal">FALSE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">文字，它可以在任何大小写被写成：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT ISNULL(null), ISNULL(Null), ISNULL(NULL);</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------- -------------- + + + --------------</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">ISNULL（null）| </font><font style="vertical-align: inherit;">ISNULL（Null）| </font><font style="vertical-align: inherit;">ISNULL（NULL）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------- -------------- + + + --------------</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">1 | </font><font style="vertical-align: inherit;">1 | </font><font style="vertical-align: inherit;">1 |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ -------------- -------------- + + + --------------</font></font><font></font>
</pre><a class="indexterm" name="idm140091691714688"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        有时可能需要或希望将引号字符（</font></font><code class="literal">"</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或</font></font><code class="literal">'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）插入JSON文档中。</font><font style="vertical-align: inherit;">假设您希望在此示例中插入一些JSON对象，这些对象包含表示有关MySQL的一些事实的句子，每个句子都与适当的关键字配对，并使用此处显示的SQL语句创建的表：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>CREATE TABLE facts (sentence JSON);</code></strong>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        这些关键词 - 句子对中有这一个：
      </font></font></p><pre data-lang="simple" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">吉祥物：MySQL吉祥物是一只名为“Sakila”的海豚。
</font></font></pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        将此作为JSON对象插入</font></font><code class="literal">facts</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表中的</font><font style="vertical-align: inherit;">一种方法
         </font><font style="vertical-align: inherit;">是使用MySQL
         </font></font><a class="link" href="functions.html#function_json-object"><code class="literal">JSON_OBJECT()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">函数。</font><font style="vertical-align: inherit;">在这种情况下，您必须使用反斜杠转义每个引号字符，如下所示：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql </font></font><strong class="userinput"><code>INSERT INTO facts VALUES</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
     &gt;&gt;   </font></font><strong class="userinput"><code>(JSON_OBJECT("mascot", "Our mascot is a dolphin named \"Sakila\"."));</code></strong>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如果将值作为JSON对象文字插入，则此方法不起作用，在这种情况下，必须使用双反斜杠转义序列，如下所示：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql </font></font><strong class="userinput"><code>INSERT INTO facts VALUES</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
     &gt;&gt;   </font></font><strong class="userinput"><code>('{"mascot": "Our mascot is a dolphin named \\"Sakila\\"."}');</code></strong>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        使用双反斜杠可防止MySQL执行转义序列处理，而是使其将字符串文字传递给存储引擎进行处理。</font><font style="vertical-align: inherit;">以刚才显示的方式插入JSON对象后，您可以通过执行简单操作看到反斜杠存在于JSON列值中</font></font><a class="link" href="sql-syntax.html#select" title="13.2.10 SELECT语法"><code class="literal">SELECT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，如下所示：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT sentence FROM facts;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- -------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">句子|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- -------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">{“吉祥物”：“我们的吉祥物是一只叫做”Sakila“的海豚。”} |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- -------- +</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        要使用</font></font><code class="literal">mascot</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">键作为</font><font style="vertical-align: inherit;">查找此特定句子
         </font><font style="vertical-align: inherit;">，可以使用column-path运算符
         </font></font><a class="link" href="functions.html#operator_json-column-path"><code class="literal">-&gt;</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，如下所示：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql&gt; SELECT col  - &gt;“$。mascot”FROM qtest;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ --------------------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">col  - &gt;“$。吉祥物”|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ --------------------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">“我们的吉祥物是一只叫做”Sakila“的海豚。” </font><font style="vertical-align: inherit;">|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ --------------------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
1排（0.00秒）</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        这样就可以保留反斜杠以及周围的引号。</font><font style="vertical-align: inherit;">要使用</font></font><code class="literal">mascot</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">键作为键</font><font style="vertical-align: inherit;">显示所需的值
         </font><font style="vertical-align: inherit;">，但不包括周围的引号或任何转义，请使用内联路径运算符
         </font></font><a class="link" href="functions.html#operator_json-inline-path"><code class="literal">-&gt;&gt;</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，如下所示：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT sentence-&gt;&gt;"$.mascot" FROM facts;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----------------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">句子 -  &gt;&gt;“$。吉祥物”|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----------------------------------------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">我们的吉祥物是一只名为“Sakila”的海豚。</font><font style="vertical-align: inherit;">|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----------------------------------------- +</font></font><font></font>
</pre>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
注意
</font></font></div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          如果</font></font><a class="link" href="server-administration.html#sqlmode_no_backslash_escapes"><code class="literal">NO_BACKSLASH_ESCAPES</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">启用</font><font style="vertical-align: inherit;">了</font><font style="vertical-align: inherit;">服务器SQL模式，则</font><font style="vertical-align: inherit;">前面的示例无法正常工作
           </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">如果设置了此模式，则可以使用单个反斜杠而不是双反斜杠来插入JSON对象文字，并保留反斜杠。</font><font style="vertical-align: inherit;">如果</font></font><code class="literal">JSON_OBJECT()</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在执行插入时</font><font style="vertical-align: inherit;">使用该</font><font style="vertical-align: inherit;">功能并且设置了此模式，则必须替换单引号和双引号，如下所示：
        </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql </font></font><strong class="userinput"><code>INSERT INTO facts VALUES</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
     &gt;&gt;</font></font><strong class="userinput"><code>(JSON_OBJECT('mascot', 'Our mascot is a dolphin named "Sakila".'));</code></strong>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          有关</font></font><a class="link" href="functions.html#function_json-unquote"><code class="literal">JSON_UNQUOTE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">此模式对JSON值中的转义字符的影响的详细信息，</font><font style="vertical-align: inherit;">请参阅该</font><font style="vertical-align: inherit;">函数</font><font style="vertical-align: inherit;">的说明
           </font><font style="vertical-align: inherit;">。
</font></font></p>
</div>

</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="json-normalization"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON值的规范化，合并和自动包装</font></font></h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091691678144"></a><a class="indexterm" name="idm140091691676688"></a><a class="indexterm" name="idm140091691675616"></a><a class="indexterm" name="idm140091691674128"></a><a class="indexterm" name="idm140091691673056"></a><a class="indexterm" name="idm140091691671568"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        解析字符串并发现它是有效的JSON文档时，它也会进行规范化。</font><font style="vertical-align: inherit;">这意味着具有复制在文档中稍后找到的键的键的成员（从左到右读取）将被丢弃。</font><font style="vertical-align: inherit;">以下</font></font><a class="link" href="functions.html#function_json-object"><code class="literal">JSON_OBJECT()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">调用</font><font style="vertical-align: inherit;">生成的对象值</font><font style="vertical-align: inherit;">仅包含第二个</font></font><code class="literal">key1</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">元素，因为该键名称在值的前面出现，如下所示：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_OBJECT('key1', 1, 'key2', 'abc', 'key1', 'def');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- ----- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">JSON_OBJECT（'​​key1'，1，'key2'，'abc'，'key1'，'def'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- ----- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">{“key1”：“def”，“key2”：“abc”} |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- ----- +</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        将值插入JSON列时也会执行规范化，如下所示：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>CREATE TABLE t1 (c1 JSON);</code></strong><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>INSERT INTO t1 VALUES</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
     &gt;      </font></font><strong class="userinput"><code>('{"x": 17, "x": "red"}'),</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
     &gt;     </font></font><strong class="userinput"><code>('{"x": 17, "x": "red", "x": [3, 5, 7]}');</code></strong><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT c1 FROM t1;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">c1 |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------ +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">{“x”：“red”} |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">{“x”：[3,5,7]} |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------ +</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        这种</font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">“ </font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">最后重复密钥获胜</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> ”</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">行为由</font></font><a class="ulink" href="https://tools.ietf.org/html/rfc7159" target="_top"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">RFC 7159</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">建议，
         </font><font style="vertical-align: inherit;">并由大多数JavaScript解析器实现。</font><font style="vertical-align: inherit;">（Bug＃86866，Bug＃26369555）
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在8.0.3之前的MySQL版本中，具有复制在文档中较早发现的密钥的密钥的成员被丢弃。</font><font style="vertical-align: inherit;">以下</font></font><a class="link" href="functions.html#function_json-object"><code class="literal">JSON_OBJECT()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">调用</font><font style="vertical-align: inherit;">生成的对象值
         </font><font style="vertical-align: inherit;">不包含第二个</font></font><code class="literal">key1</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">元素，因为该键名称出现在值的前面：
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>SELECT JSON_OBJECT('key1', 1, 'key2', 'abc', 'key1', 'def');</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- ----- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">JSON_OBJECT（'​​key1'，1，'key2'，'abc'，'key1'，'def'）|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- ----- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">{“key1”：1，“key2”：“abc”} |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------------------------------------------------- ----- +</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在MySQL 8.0.3之前，</font><font style="vertical-align: inherit;">当将值插入JSON列时，也执行了</font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">“ </font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第一次重复键获胜</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> ”</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">规范化。
      </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>CREATE TABLE t1 (c1 JSON);</code></strong><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>INSERT INTO t1 VALUES</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
     &gt;      </font></font><strong class="userinput"><code>('{"x": 17, "x": "red"}'),</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
     &gt;     </font></font><strong class="userinput"><code>('{"x": 17, "x": "red", "x": [3, 5, 7]}');</code></strong><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>SELECT c1 FROM t1;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">c1 |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----------- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">{“x”：17} |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">{“x”：17} |</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ----------- +</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL还会丢弃原始JSON文档中的键，值或元素之间的额外空格。</font><font style="vertical-align: inherit;">为了使查找更有效，它还对JSON对象的键进行排序。
        </font></font><span class="emphasis"><em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">您应该知道此排序的结果可能会发生变化，并且不保证在各个版本中保持一致。</font></font></em></span>
      </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        生成JSON值的MySQL函数（请参见
         </font></font><a class="xref" href="functions.html#json-creation-functions" title="12.17.2创建JSON值的函数"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第12.17.2节“创建JSON值的函数”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）始终返回规范化值。
</font></font></p>
<h4><a name="json-merging"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">合并JSON值</font></font></h4>
<a class="indexterm" name="idm140091691645120"></a><a class="indexterm" name="idm140091691644048"></a><a class="indexterm" name="idm140091691642976"></a><a class="indexterm" name="idm140091691641904"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL 8.0.3（及更高版本）支持两种合并算法，由函数
         </font></font><a class="link" href="functions.html#function_json-merge-preserve"><code class="literal">JSON_MERGE_PRESERVE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="functions.html#function_json-merge-patch"><code class="literal">JSON_MERGE_PATCH()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">它们处理重复键的方式不同：</font></font><a class="link" href="functions.html#function_json-merge-preserve"><code class="literal">JSON_MERGE_PRESERVE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">保留</font><font style="vertical-align: inherit;">重复键的
         </font><font style="vertical-align: inherit;">值，同时</font></font><a class="link" href="functions.html#function_json-merge-patch"><code class="literal">JSON_MERGE_PATCH()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">丢弃除最后一个值之外的所有</font><font style="vertical-align: inherit;">键
         </font><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">接下来的几段将解释这两个函数中的每一个如何处理JSON文档的不同组合（即对象和数组）的合并。
</font></font></p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
注意
</font></font></div>
<p>
          <a class="link" href="functions.html#function_json-merge-preserve"><code class="literal">JSON_MERGE_PRESERVE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">与</font></font><code class="literal">JSON_MERGE()</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以前版本的MySQL（在MySQL 8.0.3中重命名）中</font><font style="vertical-align: inherit;">的</font><font style="vertical-align: inherit;">函数</font><font style="vertical-align: inherit;">相同</font><font style="vertical-align: inherit;">。
          </font></font><code class="literal">JSON_MERGE()</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">仍然支持作为</font></font><code class="literal">JSON_MERGE_PRESERVE()</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL 8.0中</font><font style="vertical-align: inherit;">的别名</font><font style="vertical-align: inherit;">，但不推荐使用，并在将来的版本中删除。
</font></font></p>
</div>
<p><b><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">合并数组。&nbsp;</font></font></b><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          在组合多个数组的上下文中，数组合并为单个数组。
          </font></font><code class="literal">JSON_MERGE_PRESERVE()</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">通过将稍后命名的数组连接到第一个数组的末尾来实现此目的。</font></font><code class="literal">JSON_MERGE_PATCH()</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">将每个参数视为由单个元素组成的数组（因此将0作为其索引），然后应用</font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">“ </font></font><span class="quote"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">最后重复键获胜</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> ”</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">逻辑以仅选择最后一个参数。</font><font style="vertical-align: inherit;">您可以比较此查询显示的结果：
        </font></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">mysql&gt; </font></font><strong class="userinput"><code>SELECT</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    - &gt;    </font></font><strong class="userinput"><code>JSON_MERGE_PRESERVE('[1, 2]', '["a", "b", "c"]', '[true, false]') AS Preserve,</code></strong><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    - &gt;   </font></font><strong class="userinput"><code>JSON_MERGE_PATCH('[1, 2]', '["a", "b", "c"]', '[true, false]') AS Patch\G</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
*************************** 1。排******************** *******</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
保留：[1,2，“a”，“b”，“c”，true，false]</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
   补丁：[true，false]</font></font><font></font>
</pre><p>
        Multiple objects when merged produce a single object.
        <code class="literal">JSON_MERGE_PRESERVE()</code> handles multiple
        objects having the same key by combining all unique values for
        that key in an array; this array is then used as the value for
        that key in the result. <code class="literal">JSON_MERGE_PATCH()</code>
        discards values for which duplicate keys are found, working from
        left to right, so that the result contains only the last value
        for that key. The following query illustrates the difference in
        the results for the duplicate key <code class="literal">a</code>:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT</code></strong>
    -&gt;   <strong class="userinput"><code>JSON_MERGE_PRESERVE('{"a": 1, "b": 2}', '{"c": 3, "a": 4}', '{"c": 5, "d": 3}') AS Preserve,</code></strong>
    -&gt;   <strong class="userinput"><code>JSON_MERGE_PATCH('{"a": 3, "b": 2}', '{"c": 3, "a": 4}', '{"c": 5, "d": 3}') AS Patch\G</code></strong><font></font>
*************************** 1. row ***************************<font></font>
Preserve: {"a": [1, 4], "b": 2, "c": [3, 5], "d": 3}<font></font>
   Patch: {"a": 4, "b": 2, "c": 5, "d": 3}<font></font>
</pre><p>
        Nonarray values used in a context that requires an array value
        are autowrapped: The value is surrounded by <code class="literal">[</code>
        and <code class="literal">]</code> characters to convert it to an array.
        In the following statement, each argument is autowrapped as an
        array (<code class="literal">[1]</code>, <code class="literal">[2]</code>). These
        are then merged to produce a single result array; as in the
        previous two cases, <code class="literal">JSON_MERGE_PRESERVE()</code>
        combines values having the same key while
        <code class="literal">JSON_MERGE_PATCH()</code> discards values for all
        duplicate keys except the last, as shown here:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT</code></strong>
	  -&gt;   <strong class="userinput"><code>JSON_MERGE_PRESERVE('1', '2') AS Preserve,</code></strong>
	  -&gt;   <strong class="userinput"><code>JSON_MERGE_PATCH('1', '2') AS Patch\G</code></strong><font></font>
*************************** 1. row ***************************<font></font>
Preserve: [1, 2]<font></font>
   Patch: 2<font></font>
</pre><p>
        Array and object values are merged by autowrapping the object as
        an array and merging the arrays by combining values or by
        <span class="quote">“<span class="quote">last duplicate key wins</span>”</span> according to the choice
        of merging function (<code class="literal">JSON_MERGE_PRESERVE()</code> or
        <code class="literal">JSON_MERGE_PATCH()</code>, respectively), as can be
        seen in this example:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT</code></strong>
	  -&gt;   <strong class="userinput"><code>JSON_MERGE_PRESERVE('[10, 20]', '{"a": "x", "b": "y"}') AS Preserve,</code></strong>
	  -&gt;   <strong class="userinput"><code>JSON_MERGE_PATCH('[10, 20]', '{"a": "x", "b": "y"}') AS Patch\G</code></strong><font></font>
*************************** 1. row ***************************<font></font>
Preserve: [10, 20, {"a": "x", "b": "y"}]<font></font>
   Patch: {"a": "x", "b": "y"}<font></font>
</pre>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="json-paths"></a>Searching and Modifying JSON Values</h3>

</div>

</div>

</div>
<p>
        A JSON path expression selects a value within a JSON document.
      </p><p>
        Path expressions are useful with functions that extract parts of
        or modify a JSON document, to specify where within that document
        to operate. For example, the following query extracts from a
        JSON document the value of the member with the
        <code class="literal">name</code> key:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name');</code></strong><font></font>
+---------------------------------------------------------+<font></font>
| JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name') |<font></font>
+---------------------------------------------------------+<font></font>
| "Aztalan"                                               |<font></font>
+---------------------------------------------------------+<font></font>
</pre><p>
        Path syntax uses a leading <code class="literal">$</code> character to
        represent the JSON document under consideration, optionally
        followed by selectors that indicate successively more specific
        parts of the document:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            A period followed by a key name names the member in an
            object with the given key. The key name must be specified
            within double quotation marks if the name without quotes is
            not legal within path expressions (for example, if it
            contains a space).
          </p></li><li class="listitem"><p>
            <code class="literal">[<em class="replaceable"><code>N</code></em>]</code> appended
            to a <em class="replaceable"><code>path</code></em> that selects an array
            names the value at position <em class="replaceable"><code>N</code></em>
            within the array. Array positions are integers beginning
            with zero. If <em class="replaceable"><code>path</code></em> does not
            select an array value, <em class="replaceable"><code>path</code></em>[0]
            evaluates to the same value as
            <em class="replaceable"><code>path</code></em>:
          </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_SET('"x"', '$[0]', 'a');</code></strong><font></font>
+------------------------------+<font></font>
| JSON_SET('"x"', '$[0]', 'a') |<font></font>
+------------------------------+<font></font>
| "a"                          |<font></font>
+------------------------------+<font></font>
1 row in set (0.00 sec)<font></font>
</pre></li><li class="listitem"><p>
            <code class="literal">[<em class="replaceable"><code>M</code></em> to
            <em class="replaceable"><code>N</code></em>]</code> specifies a subset
            or range of array values starting with the value at position
            <em class="replaceable"><code>M</code></em>, and ending with the value at
            position <em class="replaceable"><code>N</code></em>.
          </p><p>
            <code class="literal">last</code> is supported as a synonym for the
            index of the rightmost array element. Relative addressing of
            array elements is also supported. If
            <em class="replaceable"><code>path</code></em> does not select an array
            value, <em class="replaceable"><code>path</code></em>[last] evaluates to
            the same value as <em class="replaceable"><code>path</code></em>, as shown
            later in this section (see
            <a class="xref" href="data-types.html#json-paths-last" title="最右边的数组元素">Rightmost array element</a>).
          </p></li><li class="listitem"><p>
            Paths can contain <code class="literal">*</code> or
            <code class="literal">**</code> wildcards:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                <code class="literal">.[*]</code> evaluates to the values of all
                members in a JSON object.
              </p></li><li class="listitem"><p>
                <code class="literal">[*]</code> evaluates to the values of all
                elements in a JSON array.
              </p></li><li class="listitem"><p>
                <code class="literal"><em class="replaceable"><code>prefix</code></em>**<em class="replaceable"><code>suffix</code></em></code>
                evaluates to all paths that begin with the named prefix
                and end with the named suffix.
</p></li></ul>
</div>
</li><li class="listitem"><p>
            A path that does not exist in the document (evaluates to
            nonexistent data) evaluates to <code class="literal">NULL</code>.
</p></li></ul>
</div>
<p>
        Let <code class="literal">$</code> refer to this JSON array with three
        elements:
      </p><pre data-lang="json" class="programlisting">[3, {"a": [5, 6], "b": 10}, [99, 100]]
</pre><p>
        Then:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">$[0]</code> evaluates to <code class="literal">3</code>.
          </p></li><li class="listitem"><p>
            <code class="literal">$[1]</code> evaluates to <code class="literal">{"a": [5, 6],
            "b": 10}</code>.
          </p></li><li class="listitem"><p>
            <code class="literal">$[2]</code> evaluates to <code class="literal">[99,
            100]</code>.
          </p></li><li class="listitem"><p>
            <code class="literal">$[3]</code> evaluates to <code class="literal">NULL</code>
            (it refers to the fourth array element, which does not
            exist).
</p></li></ul>
</div>
<p>
        Because <code class="literal">$[1]</code> and <code class="literal">$[2]</code>
        evaluate to nonscalar values, they can be used as the basis for
        more-specific path expressions that select nested values.
        Examples:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">$[1].a</code> evaluates to <code class="literal">[5,
            6]</code>.
          </p></li><li class="listitem"><p>
            <code class="literal">$[1].a[1]</code> evaluates to
            <code class="literal">6</code>.
          </p></li><li class="listitem"><p>
            <code class="literal">$[1].b</code> evaluates to
            <code class="literal">10</code>.
          </p></li><li class="listitem"><p>
            <code class="literal">$[2][0]</code> evaluates to
            <code class="literal">99</code>.
</p></li></ul>
</div>
<p>
        As mentioned previously, path components that name keys must be
        quoted if the unquoted key name is not legal in path
        expressions. Let <code class="literal">$</code> refer to this value:
      </p><pre data-lang="json" class="programlisting">{"a fish": "shark", "a bird": "sparrow"}
</pre><p>
        The keys both contain a space and must be quoted:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">$."a fish"</code> evaluates to
            <code class="literal">shark</code>.
          </p></li><li class="listitem"><p>
            <code class="literal">$."a bird"</code> evaluates to
            <code class="literal">sparrow</code>.
</p></li></ul>
</div>
<p>
        Paths that use wildcards evaluate to an array that can contain
        multiple values:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.*');</code></strong><font></font>
+---------------------------------------------------------+<font></font>
| JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.*') |<font></font>
+---------------------------------------------------------+<font></font>
| [1, 2, [3, 4, 5]]                                       |<font></font>
+---------------------------------------------------------+<font></font>
mysql&gt; <strong class="userinput"><code>SELECT JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.c[*]');</code></strong><font></font>
+------------------------------------------------------------+<font></font>
| JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.c[*]') |<font></font>
+------------------------------------------------------------+<font></font>
| [3, 4, 5]                                                  |<font></font>
+------------------------------------------------------------+<font></font>
</pre><p>
        In the following example, the path <code class="literal">$**.b</code>
        evaluates to multiple paths (<code class="literal">$.a.b</code> and
        <code class="literal">$.c.b</code>) and produces an array of the matching
        path values:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_EXTRACT('{"a": {"b": 1}, "c": {"b": 2}}', '$**.b');</code></strong><font></font>
+---------------------------------------------------------+<font></font>
| JSON_EXTRACT('{"a": {"b": 1}, "c": {"b": 2}}', '$**.b') |<font></font>
+---------------------------------------------------------+<font></font>
| [1, 2]                                                  |<font></font>
+---------------------------------------------------------+<font></font>
</pre><p><a name="json-paths-ranges"></a><b>Ranges from JSON arrays.&nbsp;</b>
          You can use ranges with the <code class="literal">to</code> keyword to
          specify subsets of JSON arrays. For example, <code class="literal">$[1 to
          3]</code> includes the second, third, and fourth elements
          of an array, as shown here:
        </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[1 to 3]');</code></strong><font></font>
+----------------------------------------------+<font></font>
| JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[1 to 3]') |<font></font>
+----------------------------------------------+<font></font>
| [2, 3, 4]                                    |<font></font>
+----------------------------------------------+<font></font>
1 row in set (0.00 sec)<font></font>
</pre><p>
        The syntax is <code class="literal"><em class="replaceable"><code>M</code></em> to
        <em class="replaceable"><code>N</code></em></code>, where
        <em class="replaceable"><code>M</code></em> and <em class="replaceable"><code>N</code></em>
        are, respectively, the first and last indexes of a range of
        elements from a JSON array. <em class="replaceable"><code>N</code></em> must be
        greater than <em class="replaceable"><code>M</code></em>;
        <em class="replaceable"><code>M</code></em> must be greater than or equal to 0.
        Array elements are indexed beginning with 0.
      </p><p>
        You can use ranges in contexts where wildcards are supported.
      </p><p><a name="json-paths-last"></a><b>Rightmost array element.&nbsp;</b>
          The <code class="literal">last</code> keyword is supported as a synonym
          for the index of the last element in an array. Expressions of
          the form <code class="literal">last -
          <em class="replaceable"><code>N</code></em></code> can be used for
          relative addressing, and within range definitions, like this:
        </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[last-3 to last-1]');</code></strong><font></font>
+--------------------------------------------------------+<font></font>
| JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[last-3 to last-1]') |<font></font>
+--------------------------------------------------------+<font></font>
| [2, 3, 4]                                              |<font></font>
+--------------------------------------------------------+<font></font>
1 row in set (0.01 sec)<font></font>
</pre><p>
        If the path is evaluated against a value that is not an array,
        the result of the evaluation is the same as if the value had
        been wrapped in a single-element array:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; SELECT JSON_REPLACE('"Sakila"', '$[last]', 10);<font></font>
+-----------------------------------------+<font></font>
| JSON_REPLACE('"Sakila"', '$[last]', 10) |<font></font>
+-----------------------------------------+<font></font>
| 10                                      |<font></font>
+-----------------------------------------+<font></font>
1 row in set (0.00 sec)<font></font>
</pre><p>
        You can use
        <a class="link" href="functions.html#operator_json-column-path"><code class="literal"><em class="replaceable"><code>column</code></em>-&gt;<em class="replaceable"><code>path</code></em></code></a>
        with a JSON column identifier and JSON path expression as a
        synonym for
        <a class="link" href="functions.html#function_json-extract"><code class="literal">JSON_EXTRACT(<em class="replaceable"><code>column</code></em>,
        <em class="replaceable"><code>path</code></em>)</code></a>. See
        <a class="xref" href="functions.html#json-search-functions" title="12.17.3搜索JSON值的函数">Section&nbsp;12.17.3, “Functions That Search JSON Values”</a>, for more information.
        See also <a class="xref" href="sql-syntax.html#json-column-indirect-index" title="索引生成的列以提供JSON列索引">Indexing a Generated Column to Provide a JSON Column Index</a>.
      </p><p>
        Some functions take an existing JSON document, modify it in some
        way, and return the resulting modified document. Path
        expressions indicate where in the document to make changes. For
        example, the <a class="link" href="functions.html#function_json-set"><code class="literal">JSON_SET()</code></a>,
        <a class="link" href="functions.html#function_json-insert"><code class="literal">JSON_INSERT()</code></a>, and
        <a class="link" href="functions.html#function_json-replace"><code class="literal">JSON_REPLACE()</code></a> functions each
        take a JSON document, plus one or more path-value pairs that
        describe where to modify the document and the values to use. The
        functions differ in how they handle existing and nonexisting
        values within the document.
      </p><p>
        Consider this document:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; SET @j = '["a", {"b": [true, false]}, [10, 20]]';
</pre><p>
        <a class="link" href="functions.html#function_json-set"><code class="literal">JSON_SET()</code></a> replaces values for
        paths that exist and adds values for paths that do not exist:.
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_SET(@j, '$[1].b[0]', 1, '$[2][2]', 2);</code></strong><font></font>
+--------------------------------------------+<font></font>
| JSON_SET(@j, '$[1].b[0]', 1, '$[2][2]', 2) |<font></font>
+--------------------------------------------+<font></font>
| ["a", {"b": [1, false]}, [10, 20, 2]]      |<font></font>
+--------------------------------------------+<font></font>
</pre><p>
        In this case, the path <code class="literal">$[1].b[0]</code> selects an
        existing value (<code class="literal">true</code>), which is replaced with
        the value following the path argument (<code class="literal">1</code>).
        The path <code class="literal">$[2][2]</code> does not exist, so the
        corresponding value (<code class="literal">2</code>) is added to the value
        selected by <code class="literal">$[2]</code>.
      </p><p>
        <a class="link" href="functions.html#function_json-insert"><code class="literal">JSON_INSERT()</code></a> adds new values but
        does not replace existing values:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_INSERT(@j, '$[1].b[0]', 1, '$[2][2]', 2);</code></strong><font></font>
+-----------------------------------------------+<font></font>
| JSON_INSERT(@j, '$[1].b[0]', 1, '$[2][2]', 2) |<font></font>
+-----------------------------------------------+<font></font>
| ["a", {"b": [true, false]}, [10, 20, 2]]      |<font></font>
+-----------------------------------------------+<font></font>
</pre><p>
        <a class="link" href="functions.html#function_json-replace"><code class="literal">JSON_REPLACE()</code></a> replaces existing
        values and ignores new values:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_REPLACE(@j, '$[1].b[0]', 1, '$[2][2]', 2);</code></strong><font></font>
+------------------------------------------------+<font></font>
| JSON_REPLACE(@j, '$[1].b[0]', 1, '$[2][2]', 2) |<font></font>
+------------------------------------------------+<font></font>
| ["a", {"b": [1, false]}, [10, 20]]             |<font></font>
+------------------------------------------------+<font></font>
</pre><p>
        The path-value pairs are evaluated left to right. The document
        produced by evaluating one pair becomes the new value against
        which the next pair is evaluated.
      </p><p>
        <code class="literal">JSON_REMOVE()</code> takes a JSON document and one
        or more paths that specify values to be removed from the
        document. The return value is the original document minus the
        values selected by paths that exist within the document:
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>SELECT JSON_REMOVE(@j, '$[2]', '$[1].b[1]', '$[1].b[1]');</code></strong><font></font>
+---------------------------------------------------+<font></font>
| JSON_REMOVE(@j, '$[2]', '$[1].b[1]', '$[1].b[1]') |<font></font>
+---------------------------------------------------+<font></font>
| ["a", {"b": [true]}]                              |<font></font>
+---------------------------------------------------+<font></font>
</pre><p>
        The paths have these effects:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">$[2]</code> matches <code class="literal">[10, 20]</code>
            and removes it.
          </p></li><li class="listitem"><p>
            The first instance of <code class="literal">$[1].b[1]</code> matches
            <code class="literal">false</code> in the <code class="literal">b</code> element
            and removes it.
          </p></li><li class="listitem"><p>
            The second instance of <code class="literal">$[1].b[1]</code> matches
            nothing: That element has already been removed, the path no
            longer exists, and has no effect.
</p></li></ul>
</div>

</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="json-path-syntax"></a>JSON Path Syntax</h3>

</div>

</div>

</div>
<p>
        Many of the JSON functions supported by MySQL and described
        elsewhere in this Manual (see <a class="xref" href="functions.html#json-functions" title="12.17 JSON函数">Section&nbsp;12.17, “JSON Functions”</a>)
        require a path expression in order to identify a specific
        element in a JSON document. A path consists of the path's
        scope followed by one or more path legs. For paths used in MySQL
        JSON functions, the scope is always the document being searched
        or otherwise operated on, represented by a leading
        <code class="literal">$</code> character. Path legs are separated by
        period characters (<code class="literal">.</code>). Cells in arrays are
        represented by
        <code class="literal">[<em class="replaceable"><code>N</code></em>]</code>, where
        <em class="replaceable"><code>N</code></em> is a non-negative integer. Names of
        keys must be double-quoted strings or valid ECMAScript
        identifiers (see
        <code class="uri">http://www.ecma-international.org/ecma-262/5.1/#sec-7.6</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）。</font><font style="vertical-align: inherit;">路径表达式，如JSON文本，应该使用的编码
         </font></font><code class="literal">ascii</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，</font></font><code class="literal">utf8</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
         </font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符集。</font><font style="vertical-align: inherit;">其他字符编码被隐式强制执行</font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">完整语法如下所示：
      </font></font></p><pre data-lang="clike" class="programlisting"><em class="replaceable"><code>pathExpression</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
     </font></font><em class="replaceable"><code>scope</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">[（</font></font><em class="replaceable"><code>pathLeg</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）*]</font></font><font></font>
<font></font>
<em class="replaceable"><code>pathLeg</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
     </font></font><em class="replaceable"><code>member</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">| </font></font><em class="replaceable"><code>arrayLocation</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">|</font></font><em class="replaceable"><code>doubleAsterisk</code></em><font></font>
<font></font>
<em class="replaceable"><code>member</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
     </font></font><em class="replaceable"><code>period</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（</font></font><em class="replaceable"><code>keyName</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">| </font></font><em class="replaceable"><code>asterisk</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）</font></font><font></font>
<font></font>
<em class="replaceable"><code>arrayLocation</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
     </font></font><em class="replaceable"><code>leftBracket</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（</font></font><em class="replaceable"><code>nonNegativeInteger</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">| </font></font><em class="replaceable"><code>asterisk</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）</font></font><em class="replaceable"><code>rightBracket</code></em><font></font>
<font></font>
<em class="replaceable"><code>keyName</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：
     </font></font><em class="replaceable"><code>ESIdentifier</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">|</font></font><em class="replaceable"><code>doubleQuotedString</code></em><font></font>
<font></font>
<em class="replaceable"><code>doubleAsterisk</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    '**'</font></font><font></font>
<font></font>
<em class="replaceable"><code>period</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    ''</font></font><font></font>
<font></font>
<em class="replaceable"><code>asterisk</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    '*'</font></font><font></font>
<font></font>
<em class="replaceable"><code>leftBracket</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    '['</font></font><font></font>
<font></font>
<em class="replaceable"><code>rightBracket</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">：</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
    ']'</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        如前所述，在MySQL中，路径的范围始终是正在操作的文档，表示为
         </font></font><code class="literal">$</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">您可以</font></font><code class="literal">'$'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">在JSON路径表达式中用作文档的synonynm。
</font></font></p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
注意
</font></font></div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
          某些实现支持JSON路径范围的列引用; </font><font style="vertical-align: inherit;">目前，MySQL不支持这些。
</font></font></p>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        通配符</font></font><code class="literal">*</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">**</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        令牌使用如下：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">.*</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> 表示对象中所有成员的值。
          </font></font></p></li><li class="listitem"><p>
            <code class="literal">[*]</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> 表示数组中所有单元格的值。
          </font></font></p></li><li class="listitem"><p>
            <code class="literal">[<em class="replaceable"><code>prefix</code></em>]**<em class="replaceable"><code>suffix</code></em></code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            表示以...开头</font></font><em class="replaceable"><code>prefix</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和结尾的
             </font><font style="vertical-align: inherit;">所有路径
             </font></font><em class="replaceable"><code>suffix</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
            </font></font><em class="replaceable"><code>prefix</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是可选的，虽然
             </font></font><em class="replaceable"><code>suffix</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是必需的; </font><font style="vertical-align: inherit;">换句话说，路径可能不会结束</font></font><code class="literal">**</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            另外，路径可能不包含序列
             </font></font><code class="literal">***</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        对于路径语法的例子，见该采取的路径作为参数，例如各种JSON功能的说明
         </font></font><a class="link" href="functions.html#function_json-contains-path"><code class="literal">JSON_CONTAINS_PATH()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="functions.html#function_json-set"><code class="literal">JSON_SET()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="functions.html#function_json-replace"><code class="literal">JSON_REPLACE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">有关使用</font></font><code class="literal">*</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><code class="literal">**</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">通配符的</font><font style="vertical-align: inherit;">示例</font><font style="vertical-align: inherit;">，请参阅该</font></font><a class="link" href="functions.html#function_json-search"><code class="literal">JSON_SEARCH()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">函数</font><font style="vertical-align: inherit;">的说明
         </font><font style="vertical-align: inherit;">。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        MySQL 8.0.2及更高版本还支持使用</font></font><code class="literal">to</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">关键字（例如
         </font></font><code class="literal">$[2 to 10]</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）</font><font style="vertical-align: inherit;">的JSON数组子集的范围表示法</font><font style="vertical-align: inherit;">，以及
         </font></font><code class="literal">last</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">关键字作为数组最右边元素的同义词。</font><font style="vertical-align: inherit;">有关</font><font style="vertical-align: inherit;">更多信息和示例，</font><font style="vertical-align: inherit;">请参阅</font></font><a class="xref" href="data-types.html#json-paths" title="搜索和修改JSON值"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">搜索和修改JSON值</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="json-comparison"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">JSON值的比较和排序</font></font></h3>

</div>

</div>

</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        JSON值可以使用进行比较
         </font></font><a class="link" href="functions.html#operator_equal"><code class="literal">=</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="functions.html#operator_less-than"><code class="literal">&lt;</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="functions.html#operator_less-than-or-equal"><code class="literal">&lt;=</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="functions.html#operator_greater-than"><code class="literal">&gt;</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="functions.html#operator_greater-than-or-equal"><code class="literal">&gt;=</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="functions.html#operator_not-equal"><code class="literal">&lt;&gt;</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="functions.html#operator_not-equal"><code class="literal">!=</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，和
         </font></font><a class="link" href="functions.html#operator_equal-to"><code class="literal">&lt;=&gt;</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        运营商。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        JSON值尚不支持以下比较运算符和函数：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <a class="link" href="functions.html#operator_between"><code class="literal">BETWEEN</code></a>
          </p></li><li class="listitem"><p>
            <a class="link" href="functions.html#operator_in"><code class="literal">IN()</code></a>
          </p></li><li class="listitem"><p>
            <a class="link" href="functions.html#function_greatest"><code class="literal">GREATEST()</code></a>
          </p></li><li class="listitem"><p>
            <a class="link" href="functions.html#function_least"><code class="literal">LEAST()</code></a>
</p></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        刚才列出的比较运算符和函数的解决方法是将JSON值转换为本机MySQL数值或字符串数​​据类型，以便它们具有一致的非JSON标量类型。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        JSON值的比较发生在两个级别。</font><font style="vertical-align: inherit;">第一级比较基于比较值的JSON类型。</font><font style="vertical-align: inherit;">如果类型不同，则比较结果仅由哪种类型具有更高优先级来确定。</font><font style="vertical-align: inherit;">如果这两个值具有相同的JSON类型，则使用特定于类型的规则进行第二级比较。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        以下列表显示了JSON类型的优先级，从最高优先级到最低优先级。</font><font style="vertical-align: inherit;">（类型名称是</font></font><a class="link" href="functions.html#function_json-type"><code class="literal">JSON_TYPE()</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        函数</font><font style="vertical-align: inherit;">返回的类型名称</font><font style="vertical-align: inherit;">。）一行显示的类型具有相同的优先级。</font><font style="vertical-align: inherit;">列表中前面列出的任何具有JSON类型的值都比列表中稍后列出的具有JSON类型的任何值都要大。
      </font></font></p><pre data-lang="simple" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">BLOB</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
BIT</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
不透明</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
约会时间</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
时间</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
日期</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
布尔</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
ARRAY</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
宾语</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
串</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
INTEGER，DOUBLE</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
空值</font></font><font></font>
</pre><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        对于具有相同优先级的JSON值，比较规则是特定于类型的：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">BLOB</code>
          </p><p><font style="vertical-align: inherit;"></font><em class="replaceable"><code>N</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">比较两个值</font><font style="vertical-align: inherit;">
            的第一个</font><font style="vertical-align: inherit;">字节，其中</font></font><em class="replaceable"><code>N</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是较短值中的字节数。</font><font style="vertical-align: inherit;">如果</font></font><em class="replaceable"><code>N</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">两个值</font><font style="vertical-align: inherit;">的第一个
             </font><font style="vertical-align: inherit;">字节相同，则在较长值之前排序较短的值。
          </font></font></p></li><li class="listitem"><p>
            <code class="literal">BIT</code>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            同样的规则</font></font><code class="literal">BLOB</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
          </font></font></p></li><li class="listitem"><p>
            <code class="literal">OPAQUE</code>
          </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            同样的规则</font></font><code class="literal">BLOB</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
            </font></font><code class="literal">OPAQUE</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值是未归类为其他类型之一的值。
          </font></font></p></li><li class="listitem"><p>
            <code class="literal">DATETIME</code>
          </p><p>
            A value that represents an earlier point in time is ordered
            before a value that represents a later point in time. If two
            values originally come from the MySQL
            <code class="literal">DATETIME</code> and <code class="literal">TIMESTAMP</code>
            types, respectively, they are equal if they represent the
            same point in time.
          </p></li><li class="listitem"><p>
            <code class="literal">TIME</code>
          </p><p>
            The smaller of two time values is ordered before the larger
            one.
          </p></li><li class="listitem"><p>
            <code class="literal">DATE</code>
          </p><p>
            The earlier date is ordered before the more recent date.
          </p></li><li class="listitem"><p>
            <code class="literal">ARRAY</code>
          </p><p>
            Two JSON arrays are equal if they have the same length and
            values in corresponding positions in the arrays are equal.
          </p><p>
            If the arrays are not equal, their order is determined by
            the elements in the first position where there is a
            difference. The array with the smaller value in that
            position is ordered first. If all values of the shorter
            array are equal to the corresponding values in the longer
            array, the shorter array is ordered first.
          </p><p>
            Example:
          </p><pre data-lang="simple" class="programlisting">[] &lt; ["a"] &lt; ["ab"] &lt; ["ab", "cd", "ef"] &lt; ["ab", "ef"]
</pre></li><li class="listitem"><p>
            <code class="literal">BOOLEAN</code>
          </p><p>
            The JSON false literal is less than the JSON true literal.
          </p></li><li class="listitem"><p>
            <code class="literal">OBJECT</code>
          </p><p>
            Two JSON objects are equal if they have the same set of
            keys, and each key has the same value in both objects.
          </p><p>
            Example:
          </p><pre data-lang="simple" class="programlisting">{"a": 1, "b": 2} = {"b": 2, "a": 1}
</pre><p>
            The order of two objects that are not equal is unspecified
            but deterministic.
          </p></li><li class="listitem"><p>
            <code class="literal">STRING</code>
          </p><p>
            Strings are ordered lexically on the first
            <em class="replaceable"><code>N</code></em> bytes of the
            <code class="literal">utf8mb4</code> representation of the two strings
            being compared, where <em class="replaceable"><code>N</code></em> is the
            length of the shorter string. If the first
            <em class="replaceable"><code>N</code></em> bytes of the two strings are
            identical, the shorter string is considered smaller than the
            longer string.
          </p><p>
            Example:
          </p><pre data-lang="simple" class="programlisting">"a" &lt; "ab" &lt; "b" &lt; "bc"
</pre><p>
            This ordering is equivalent to the ordering of SQL strings
            with collation <code class="literal">utf8mb4_bin</code>. Because
            <code class="literal">utf8mb4_bin</code> is a binary collation,
            comparison of JSON values is case-sensitive:
          </p><pre data-lang="simple" class="programlisting">"A" &lt; "a"
</pre></li><li class="listitem"><p>
            <code class="literal">INTEGER</code>, <code class="literal">DOUBLE</code>
          </p><p>
            JSON values can contain exact-value numbers and
            approximate-value numbers. For a general discussion of these
            types of numbers, see <a class="xref" href="language-structure.html#number-literals" title="9.1.2数字文字">Section&nbsp;9.1.2, “Numeric Literals”</a>.
          </p><p>
            The rules for comparing native MySQL numeric types are
            discussed in <a class="xref" href="functions.html#type-conversion" title="12.2表达式评估中的类型转换">Section&nbsp;12.2, “Type Conversion in Expression Evaluation”</a>, but the
            rules for comparing numbers within JSON values differ
            somewhat:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                In a comparison between two columns that use the native
                MySQL <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT</code></a> and
                <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a> numeric types,
                respectively, it is known that all comparisons involve
                an integer and a double, so the integer is converted to
                double for all rows. That is, exact-value numbers are
                converted to approximate-value numbers.
              </p></li><li class="listitem"><p>
                On the other hand, if the query compares two JSON
                columns containing numbers, it cannot be known in
                advance whether numbers will be integer or double. To
                provide the most consistent behavior across all rows,
                MySQL converts approximate-value numbers to exact-value
                numbers. The resulting ordering is consistent and does
                not lose precision for the exact-value numbers. For
                example, given the scalars 9223372036854775805,
                9223372036854775806, 9223372036854775807 and
                9.223372036854776e18, the order is such as this:
              </p><pre data-lang="sql" class="programlisting">9223372036854775805 &lt; 9223372036854775806 &lt; 9223372036854775807<font></font>
&lt; 9.223372036854776e18 = 9223372036854776000 &lt; 9223372036854776001<font></font>
</pre></li></ul>
</div>
<p>
            Were JSON comparisons to use the non-JSON numeric comparison
            rules, inconsistent ordering could occur. The usual MySQL
            comparison rules for numbers yield these orderings:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                Integer comparison:
              </p><pre data-lang="sql" class="programlisting">9223372036854775805 &lt; 9223372036854775806 &lt; 9223372036854775807
</pre><p>
                (not defined for 9.223372036854776e18)
              </p></li><li class="listitem"><p>
                Double comparison:
              </p><pre data-lang="simple" class="programlisting">9223372036854775805 = 9223372036854775806 = 9223372036854775807 = 9.223372036854776e18
</pre></li></ul>
</div>
</li></ul>
</div>
<p>
        For comparison of any JSON value to SQL <code class="literal">NULL</code>,
        the result is <code class="literal">UNKNOWN</code>.
      </p><p>
        For comparison of JSON and non-JSON values, the non-JSON value
        is converted to JSON according to the rules in the following
        table, then the values compared as described previously.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="json-converting-between-types"></a>Converting between JSON and non-JSON values</h3>

</div>

</div>

</div>
<p>
        The following table provides a summary of the rules that MySQL
        follows when casting between JSON values and values of other
        types:
</p>
<div class="oracle-all">
<a name="json-conversion-rules"></a><p class="title"><b>Table&nbsp;11.3&nbsp;JSON Conversion Rules</b></p>
<div class="oracle-all-contents">
<table summary="Conversion rules for the JSON data type"><colgroup><col width="20%"><col width="40%"><col width="40%"></colgroup><thead><tr>
            <th scope="col">other type</th>
            <th scope="col">CAST(other type AS JSON)</th>
            <th scope="col">CAST(JSON AS other type)</th>
          </tr></thead><tbody><tr>
            <td scope="row">JSON</td>
            <td>No change</td>
            <td>No change</td>
          </tr><tr>
            <td scope="row">utf8 character type (<code class="literal">utf8mb4</code>,
              <code class="literal">utf8</code>, <code class="literal">ascii</code>)</td>
            <td>The string is parsed into a JSON value.</td>
            <td>The JSON value is serialized into a <code class="literal">utf8mb4</code> string.</td>
          </tr><tr>
            <td scope="row">Other character types</td>
            <td>Other character encodings are implicitly converted to
              <code class="literal">utf8mb4</code> and treated as described for
              utf8 character type.</td>
            <td>The JSON value is serialized into a <code class="literal">utf8mb4</code> string,
              then cast to the other character encoding. The result may
              not be meaningful.</td>
          </tr><tr>
            <td scope="row"><code class="literal">NULL</code></td>
            <td>Results in a <code class="literal">NULL</code> value of type JSON.</td>
            <td>Not applicable.</td>
          </tr><tr>
            <td scope="row">Geometry types</td>
            <td>The geometry value is converted into a JSON document by calling
              <a class="link" href="functions.html#function_st-asgeojson"><code class="literal">ST_AsGeoJSON()</code></a>.</td>
            <td>Illegal operation. Workaround: Pass the result of
              <a class="link" href="functions.html#function_cast"><code class="literal">CAST(<em class="replaceable"><code>json_val</code></em>
              AS CHAR)</code></a> to
              <a class="link" href="functions.html#function_st-geomfromgeojson"><code class="literal">ST_GeomFromGeoJSON()</code></a>.</td>
          </tr><tr>
            <td scope="row">All other types</td>
            <td>Results in a JSON document consisting of a single scalar value.</td>
            <td>Succeeds if the JSON document consists of a single scalar value of the
              target type and that scalar value can be cast to the
              target type. Otherwise, returns <code class="literal">NULL</code>
              and produces a warning.</td>
</tr></tbody></table>
</div>

</div>
<br class="oracle-all-break"><p>
        <code class="literal">ORDER BY</code> and <code class="literal">GROUP BY</code> for
        JSON values works according to these principles:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Ordering of scalar JSON values uses the same rules as in the
            preceding discussion.
          </p></li><li class="listitem"><p>
            For ascending sorts, SQL <code class="literal">NULL</code> orders
            before all JSON values, including the JSON null literal; for
            descending sorts, SQL <code class="literal">NULL</code> orders after
            all JSON values, including the JSON null literal.
          </p></li><li class="listitem"><p>
            Sort keys for JSON values are bound by the value of the
            <a class="link" href="server-administration.html#sysvar_max_sort_length"><code class="literal">max_sort_length</code></a> system
            variable, so keys that differ only after the first
            <a class="link" href="server-administration.html#sysvar_max_sort_length"><code class="literal">max_sort_length</code></a> bytes
            compare as equal.
          </p></li><li class="listitem"><p>
            Sorting of nonscalar values is not currently supported and a
            warning occurs.
</p></li></ul>
</div>
<p>
        For sorting, it can be beneficial to cast a JSON scalar to some
        other native MySQL type. For example, if a column named
        <code class="literal">jdoc</code> contains JSON objects having a member
        consisting of an <code class="literal">id</code> key and a nonnegative
        value, use this expression to sort by <code class="literal">id</code>
        values:
      </p><pre data-lang="sql" class="programlisting">ORDER BY CAST(JSON_EXTRACT(jdoc, '$.id') AS UNSIGNED)
</pre><p>
        If there happens to be a generated column defined to use the
        same expression as in the <code class="literal">ORDER BY</code>, the MySQL
        optimizer recognizes that and considers using the index for the
        query execution plan. See
        <a class="xref" href="optimization.html#generated-column-index-optimizations" title="8.3.11生成列索引的优化程序使用">Section&nbsp;8.3.11, “Optimizer Use of Generated Column Indexes”</a>.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="json-aggregation"></a>Aggregation of JSON Values</h3>

</div>

</div>

</div>
<p>
        For aggregation of JSON values, SQL <code class="literal">NULL</code>
        values are ignored as for other data types.
        Non-<code class="literal">NULL</code> values are converted to a numeric
        type and aggregated, except for
        <a class="link" href="functions.html#function_min"><code class="literal">MIN()</code></a>,
        <a class="link" href="functions.html#function_max"><code class="literal">MAX()</code></a>, and
        <a class="link" href="functions.html#function_group-concat"><code class="literal">GROUP_CONCAT()</code></a>. The conversion to
        number should produce a meaningful result for JSON values that
        are numeric scalars, although (depending on the values)
        truncation and loss of precision may occur. Conversion to number
        of other JSON values may not produce a meaningful result.
</p>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="data-type-defaults"></a>11.7&nbsp;Data Type Default Values</h2>

</div>

</div>

</div>
<a class="indexterm" name="idm140091691285328"></a><a class="indexterm" name="idm140091691284288"></a><a class="indexterm" name="idm140091691282800"></a><a class="indexterm" name="idm140091691281728"></a><a class="indexterm" name="idm140091691280240"></a><a class="indexterm" name="idm140091691279168"></a><p>
      Data type specifications can have explicit or implicit default
      values.
    </p><p>
      A <code class="literal">DEFAULT <em class="replaceable"><code>value</code></em></code>
      clause in a data type specification explicitly indicates a default
      value for a column. Examples:
    </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  i     INT DEFAULT -1,<font></font>
  c     VARCHAR(10) DEFAULT '',<font></font>
  price DOUBLE(16,2) DEFAULT 0.00<font></font>
);<font></font>
</pre><a class="indexterm" name="idm140091691275056"></a><p>
      <code class="literal">SERIAL DEFAULT VALUE</code> is a special case. In the
      definition of an integer column, it is an alias for <code class="literal">NOT
      NULL AUTO_INCREMENT UNIQUE</code>.
    </p><p>
      Some aspects of explicit <code class="literal">DEFAULT</code> clause
      handling are version dependent, as described following.
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><a class="xref" href="data-types.html#data-types-defaults-explicit" title="从MySQL 8.0.13开始处理显式默认值">Handling of Explicit Defaults as of MySQL 8.0.13</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#data-types-defaults-explicit-old" title="在MySQL 8.0.13之前处理显式默认值">Handling of Explicit Defaults Prior to MySQL 8.0.13</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#data-types-defaults-implicit" title="处理隐式默认值">Handling of Implicit Defaults</a></p></li></ul>
</div>

<div class="simplesect">

<div class="titlepage">
<div>

<div class="simple">
<h3 class="title"><a name="data-types-defaults-explicit"></a>Handling of Explicit Defaults as of MySQL 8.0.13</h3>

</div>

</div>

</div>
<p>
        The default value specified in a <code class="literal">DEFAULT</code>
        clause can be a literal constant or an expression. With one
        exception, enclose expression default values within parentheses
        to distinguish them from literal constant default values.
        Examples:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t1 (<font></font>
  -- literal defaults<font></font>
  i INT         DEFAULT 0,<font></font>
  c VARCHAR(10) DEFAULT '',<font></font>
  -- expression defaults<font></font>
  f FLOAT       DEFAULT (RAND() * RAND()),<font></font>
  b BINARY(16)  DEFAULT (UUID_TO_BIN(UUID())),<font></font>
  d DATE        DEFAULT (CURRENT_DATE + INTERVAL 1 YEAR),<font></font>
  p POINT       DEFAULT (Point(0,0)),<font></font>
  j JSON        DEFAULT (JSON_ARRAY())<font></font>
);<font></font>
</pre><p>
        The exception is that, for
        <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a> and
        <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a> columns, you can specify
        the <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a> function as
        the default, without enclosing parentheses. See
        <a class="xref" href="data-types.html#timestamp-initialization" title="11.3.4 TIMESTAMP和DATETIME的自动初始化和更新">Section&nbsp;11.3.4, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”</a>.
      </p><p>
        The <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a>,
        <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a>,
        <code class="literal">GEOMETRY</code>, and
        <a class="link" href="data-types.html#json" title="11.6 JSON数据类型"><code class="literal">JSON</code></a> data types can be assigned a
        default value only if the value is written as an expression,
        even if the expression value is a literal:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            This is permitted (literal default specified as expression):
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE t2 (b BLOB DEFAULT ('abc'));
</pre></li><li class="listitem"><p>
            This produces an error (literal default not specified as
            expression):
          </p><pre data-lang="sql" class="programlisting">CREATE TABLE t2 (b BLOB DEFAULT 'abc');
</pre></li></ul>
</div>
<p>
        Expression default values must adhere to the following rules. An
        error occurs if an expression contains disallowed constructs.
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Literals, built-in functions (both deterministic and
            nondeterministic), and operators are permitted.
          </p></li><li class="listitem"><p>
            Subqueries, parameters, variables, stored functions, and
            user-defined functions are not permitted.
          </p></li><li class="listitem"><p>
            An expression default value cannot depend on a column that
            has the <code class="literal">AUTO_INCREMENT</code> attribute.
          </p></li><li class="listitem"><p>
            An expression default value for one column can refer to
            other table columns, with the exception that references to
            generated columns or columns with expression default values
            must be to columns that occur earlier in the table
            definition. That is, expression default values cannot
            contain forward references to generated columns or columns
            with expression default values.
          </p><p>
            The ordering constraint also applies to the use of
            <a class="link" href="sql-syntax.html#alter-table" title="13.1.9 ALTER TABLE语法"><code class="literal">ALTER TABLE</code></a> to reorder table
            columns. If the resulting table would have an expression
            default value that contains a forward reference to a
            generated column or column with an expression default value,
            the statement fails.
</p></li></ul>
</div>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<div class="admon-title">
Note
</div>
<p>
          If any component of an expression default value depends on the
          SQL mode, different results may occur for different uses of
          the table unless the SQL mode is the same during all uses.
</p>
</div>
<p>
        For <a class="link" href="sql-syntax.html#create-table-like" title="13.1.20.4 CREATE TABLE ... LIKE语法"><code class="literal">CREATE
        TABLE ... LIKE</code></a> and
        <a class="link" href="sql-syntax.html#create-table-select" title="13.1.20.5 CREATE TABLE ... SELECT语法"><code class="literal">CREATE
        TABLE ... SELECT</code></a>, the destination table preserves
        expression default values from the original table.
      </p><p>
        If an expression default value refers to a nondeterministic
        function, any statement that causes the expression to be
        evaluated is unsafe for statement-based replication. This
        includes statements such as
        <a class="link" href="sql-syntax.html#insert" title="13.2.6 INSERT语法"><code class="literal">INSERT</code></a>,
        <a class="link" href="sql-syntax.html#update" title="13.2.12 UPDATE语法"><code class="literal">UPDATE</code></a>, and
        <a class="link" href="sql-syntax.html#alter-table" title="13.1.9 ALTER TABLE语法"><code class="literal">ALTER TABLE</code></a>. In this situation,
        if binary logging is disabled, the statement is executed as
        normal. If binary logging is enabled and
        <a class="link" href="replication.html#sysvar_binlog_format"><code class="literal">binlog_format</code></a> is set to
        <code class="literal">STATEMENT</code>, the statement is logged and
        executed but a warning message is written to the error log,
        because replication slaves might diverge. When
        <a class="link" href="replication.html#sysvar_binlog_format"><code class="literal">binlog_format</code></a> is set to
        <code class="literal">MIXED</code> or <code class="literal">ROW</code>, the
        statement is not executed and an error message is written to the
        error log.
      </p><p>
        When inserting a new row, the default value for a column with an
        expression default can be inserted either by omitting the column
        name or by specifying the column as <code class="literal">DEFAULT</code>
        (just as for columns with literal defaults):
      </p><pre data-lang="sql" class="programlisting">mysql&gt; <strong class="userinput"><code>CREATE TABLE t4 (uid BINARY(16) DEFAULT (UUID_TO_BIN(UUID())));</code></strong>
mysql&gt; <strong class="userinput"><code>INSERT INTO t4 () VALUES();</code></strong>
mysql&gt; <strong class="userinput"><code>INSERT INTO t4 () VALUES(DEFAULT);</code></strong>
mysql&gt; <strong class="userinput"><code>SELECT BIN_TO_UUID(uid) AS uid FROM t4;</code></strong><font></font>
+--------------------------------------+<font></font>
| uid                                  |<font></font>
+--------------------------------------+<font></font>
| f1109174-94c9-11e8-971d-3bf1095aa633 |<font></font>
| f110cf9a-94c9-11e8-971d-3bf1095aa633 |<font></font>
+--------------------------------------+<font></font>
</pre><p>
        However, the use of
        <a class="link" href="functions.html#function_default"><code class="literal">DEFAULT(<em class="replaceable"><code>col_name</code></em>)</code></a>
        to specify the default value for a named column is permitted
        only for columns that have a literal default value, not for
        columns that have an expression default value.
      </p><p>
        Not all storage engines permit expression default values. For
        those that do not, an
        <a class="link" href="error-handling.html#error_er_unsupported_action_on_default_val_generated"><code class="literal">ER_UNSUPPORTED_ACTION_ON_DEFAULT_VAL_GENERATED</code></a>
        error occurs.
      </p><p>
        If a default value evaluates to a data type that differs from
        the declared column type, implicit coercion to the declared type
        occurs according to the usual MySQL type-conversion rules. See
        <a class="xref" href="functions.html#type-conversion" title="12.2表达式评估中的类型转换">Section&nbsp;12.2, “Type Conversion in Expression Evaluation”</a>.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="data-types-defaults-explicit-old"></a>Handling of Explicit Defaults Prior to MySQL 8.0.13</h3>

</div>

</div>

</div>
<p>
        With one exception, the default value specified in a
        <code class="literal">DEFAULT</code> clause must be a literal constant; it
        cannot be a function or an expression. This means, for example,
        that you cannot set the default for a date column to be the
        value of a function such as <a class="link" href="functions.html#function_now"><code class="literal">NOW()</code></a>
        or <a class="link" href="functions.html#function_current-date"><code class="literal">CURRENT_DATE</code></a>. The exception is
        that, for <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a> and
        <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a> columns, you can specify
        <a class="link" href="functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP</code></a> as the default.
        See <a class="xref" href="data-types.html#timestamp-initialization" title="11.3.4 TIMESTAMP和DATETIME的自动初始化和更新">Section&nbsp;11.3.4, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”</a>.
      </p><p>
        The <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a>,
        <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a>,
        <code class="literal">GEOMETRY</code>, and
        <a class="link" href="data-types.html#json" title="11.6 JSON数据类型"><code class="literal">JSON</code></a> data types cannot be
        assigned a default value.
      </p><p>
        If a default value evaluates to a data type that differs from
        the declared column type, implicit coercion to the declared type
        occurs according to the usual MySQL type-conversion rules. See
        <a class="xref" href="functions.html#type-conversion" title="12.2表达式评估中的类型转换">Section&nbsp;12.2, “Type Conversion in Expression Evaluation”</a>.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="data-types-defaults-implicit"></a>Handling of Implicit Defaults</h3>

</div>

</div>

</div>
<p>
        If a data type specification includes no explicit
        <code class="literal">DEFAULT</code> value, MySQL determines the default
        value as follows:
      </p><p>
        If the column can take <code class="literal">NULL</code> as a value, the
        column is defined with an explicit <code class="literal">DEFAULT
        NULL</code> clause.
      </p><p>
        If the column cannot take <code class="literal">NULL</code> as a value,
        MySQL defines the column with no explicit
        <code class="literal">DEFAULT</code> clause. Exception: If the column is
        defined as part of a <code class="literal">PRIMARY KEY</code> but not
        explicitly as <code class="literal">NOT NULL</code>, MySQL creates it as a
        <code class="literal">NOT NULL</code> column (because <code class="literal">PRIMARY
        KEY</code> columns must be <code class="literal">NOT NULL</code>).
      </p><p>
        For data entry into a <code class="literal">NOT NULL</code> column that
        has no explicit <code class="literal">DEFAULT</code> clause, if an
        <a class="link" href="sql-syntax.html#insert" title="13.2.6 INSERT语法"><code class="literal">INSERT</code></a> or
        <a class="link" href="sql-syntax.html#replace" title="13.2.9 REPLACE语法"><code class="literal">REPLACE</code></a> statement includes no
        value for the column, or an
        <a class="link" href="sql-syntax.html#update" title="13.2.12 UPDATE语法"><code class="literal">UPDATE</code></a> statement sets the column
        to <code class="literal">NULL</code>, MySQL handles the column according
        to the SQL mode in effect at the time:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            If strict SQL mode is enabled, an error occurs for
            transactional tables and the statement is rolled back. For
            nontransactional tables, an error occurs, but if this
            happens for the second or subsequent row of a multiple-row
            statement, the preceding rows will have been inserted.
          </p></li><li class="listitem"><p>
            If strict mode is not enabled, MySQL sets the column to the
            implicit default value for the column data type.
</p></li></ul>
</div>
<p>
        Suppose that a table <code class="literal">t</code> is defined as follows:
      </p><pre data-lang="sql" class="programlisting">CREATE TABLE t (i INT NOT NULL);
</pre><p>
        In this case, <code class="literal">i</code> has no explicit default, so
        in strict mode each of the following statements produce an error
        and no row is inserted. When not using strict mode, only the
        third statement produces an error; the implicit default is
        inserted for the first two statements, but the third fails
        because <a class="link" href="functions.html#function_default"><code class="literal">DEFAULT(i)</code></a> cannot produce
        a value:
      </p><pre data-lang="sql" class="programlisting">INSERT INTO t VALUES();<font></font>
INSERT INTO t VALUES(DEFAULT);<font></font>
INSERT INTO t VALUES(DEFAULT(i));<font></font>
</pre><p>
        See <a class="xref" href="server-administration.html#sql-mode" title="5.1.11服务器SQL模式">Section&nbsp;5.1.11, “Server SQL Modes”</a>.
      </p><p>
        For a given table, the <a class="link" href="sql-syntax.html#show-create-table" title="13.7.6.10 SHOW CREATE TABLE语法"><code class="literal">SHOW CREATE
        TABLE</code></a> statement displays which columns have an
        explicit <code class="literal">DEFAULT</code> clause.
      </p><p>
        Implicit defaults are defined as follows:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            For numeric types, the default is <code class="literal">0</code>, with
            the exception that for integer or floating-point types
            declared with the <code class="literal">AUTO_INCREMENT</code>
            attribute, the default is the next value in the sequence.
          </p></li><li class="listitem"><p>
            For date and time types other than
            <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a>, the default is the
            appropriate <span class="quote">“<span class="quote">zero</span>”</span> value for the type. This is
            also true for <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a> if
            the
            <a class="link" href="server-administration.html#sysvar_explicit_defaults_for_timestamp"><code class="literal">explicit_defaults_for_timestamp</code></a>
            system variable is enabled (see
            <a class="xref" href="server-administration.html#server-system-variables" title="5.1.8服务器系统变量">Section&nbsp;5.1.8, “Server System Variables”</a>). Otherwise, for
            the first <a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a> column in
            a table, the default value is the current date and time. See
            <a class="xref" href="data-types.html#date-and-time-types" title="11。3日期和时间类型">Section&nbsp;11.3, “Date and Time Types”</a>.
          </p></li><li class="listitem"><p>
            For string types other than
            <a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM</code></a>, the default value is
            the empty string. For <a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM</code></a>,
            the default is the first enumeration value.
</p></li></ul>
</div>

</div>

</div>

<div class="section">

<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="storage-requirements"></a>11.8&nbsp;Data Type Storage Requirements</h2>

</div>

</div>

</div>
<a class="indexterm" name="idm140091691157344"></a><a class="indexterm" name="idm140091691155888"></a>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><a class="xref" href="data-types.html#data-types-storage-reqs-innodb" title="InnoDB表存储要求">InnoDB Table Storage Requirements</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#data-types-storage-reqs-ndb" title="NDB表存储要求">NDB Table Storage Requirements</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#data-types-storage-reqs-numeric" title="数字类型存储要求">Numeric Type Storage Requirements</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#data-types-storage-reqs-date-time" title="日期和时间类型存储要求">Date and Time Type Storage Requirements</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#data-types-storage-reqs-strings" title="字符串类型存储要求">String Type Storage Requirements</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#data-types-storage-reqs-gis" title="空间类型存储要求">Spatial Type Storage Requirements</a></p></li><li class="listitem"><p><a class="xref" href="data-types.html#data-types-storage-reqs-json" title="JSON存储要求">JSON Storage Requirements</a></p></li></ul>
</div>
<p>
      The storage requirements for table data on disk depend on several
      factors. Different storage engines represent data types and store
      raw data differently. Table data might be compressed, either for a
      column or an entire row, complicating the calculation of storage
      requirements for a table or column.
    </p><p>
      Despite differences in storage layout on disk, the internal MySQL
      APIs that communicate and exchange information about table rows
      use a consistent data structure that applies across all storage
      engines.
    </p><p>
      This section includes guidelines and information for the storage
      requirements for each data type supported by MySQL, including the
      internal format and size for storage engines that use a fixed-size
      representation for data types. Information is listed by category
      or storage engine.
    </p><a class="indexterm" name="idm140091691144448"></a><p>
      The internal representation of a table has a maximum row size of
      65,535 bytes, even if the storage engine is capable of supporting
      larger rows. This figure excludes
      <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a> or
      <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a> columns, which contribute only
      9 to 12 bytes toward this size. For
      <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a> and
      <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a> data, the information is
      stored internally in a different area of memory than the row
      buffer. Different storage engines handle the allocation and
      storage of this data in different ways, according to the method
      they use for handling the corresponding types. For more
      information, see <a class="xref" href="storage-engines.html" title="第16章备用存储引擎">Chapter&nbsp;16, <i>Alternative Storage Engines</i></a>, and
      <a class="xref" href="restrictions.html#column-count-limit" title="C.10.4表列数和行大小的限制">Section&nbsp;C.10.4, “Limits on Table Column Count and Row Size”</a>.
</p>
<div class="simplesect">

<div class="titlepage">
<div>

<div class="simple">
<h3 class="title"><a name="data-types-storage-reqs-innodb"></a>InnoDB Table Storage Requirements</h3>
</div>
</div>
</div>
<a class="indexterm" name="idm140091691134624"></a><a class="indexterm" name="idm140091691133136"></a><p>
        See <a class="xref" href="innodb-storage-engine.html#innodb-row-format" title="15.10 InnoDB行格式">Section&nbsp;15.10, “InnoDB Row Formats”</a> for information about
        storage requirements for <code class="literal">InnoDB</code> tables.
</p>
</div>

<div class="simplesect">

<div class="titlepage">
<div>

<div class="simple">
<h3 class="title"><a name="data-types-storage-reqs-ndb"></a>NDB Table Storage Requirements</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091691128512"></a><a class="indexterm" name="idm140091691127024"></a>
<div class="important" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title">
Important
</div>
<p>
          <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> tables use
          <span class="firstterm">4-byte alignment</span>; all
          <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> data storage is done in
          multiples of 4 bytes. Thus, a column value that would
          typically take 15 bytes requires 16 bytes in an
          <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> table. For example, in
          <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> tables, the
          <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">TINYINT</code></a>,
          <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">SMALLINT</code></a>,
          <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">MEDIUMINT</code></a>, and
          <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INTEGER</code></a>
          (<a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT</code></a>) column types each require
          4 bytes storage per record due to the alignment factor.
        </p><p>
          Each <code class="literal">BIT(<em class="replaceable"><code>M</code></em>)</code>
          column takes <em class="replaceable"><code>M</code></em> bits of storage
          space. Although an individual
          <a class="link" href="data-types.html#bit-type" title="11.2.4比特值类型 -  BIT"><code class="literal">BIT</code></a> column is
          <span class="emphasis"><em>not</em></span> 4-byte aligned,
          <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> reserves 4 bytes (32 bits)
          per row for the first 1-32 bits needed for
          <code class="literal">BIT</code> columns, then another 4 bytes for bits
          33-64, and so on.
        </p><p>
          While a <code class="literal">NULL</code> itself does not require any
          storage space, <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> reserves 4
          bytes per row if the table definition contains any columns
          allowing <code class="literal">NULL</code>, up to 32
          <code class="literal">NULL</code> columns. (If an NDB Cluster table is
          defined with more than 32 <code class="literal">NULL</code> columns up
          to 64 <code class="literal">NULL</code> columns, then 8 bytes per row
          are reserved.)
</p>
</div>
<p>
        Every table using the <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> storage
        engine requires a primary key; if you do not define a primary
        key, a <span class="quote">“<span class="quote">hidden</span>”</span> primary key is created by
        <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a>. This hidden primary key
        consumes 31-35 bytes per table record.
      </p><a class="indexterm" name="idm140091691098320"></a><p>
        You can use the <a class="link" href="mysql-cluster.html#mysql-cluster-programs-ndb-size-pl" title="22.4.28 ndb_size.pl  -  NDBCLUSTER大小要求估算器"><span class="command"><strong>ndb_size.pl</strong></span></a> Perl script to
        estimate <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> storage requirements.
        It connects to a current MySQL (not NDB Cluster) database and
        creates a report on how much space that database would require
        if it used the <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> storage engine.
        See <a class="xref" href="mysql-cluster.html#mysql-cluster-programs-ndb-size-pl" title="22.4.28 ndb_size.pl  -  NDBCLUSTER大小要求估算器">Section&nbsp;22.4.28, “<span class="command"><strong>ndb_size.pl</strong></span> — NDBCLUSTER Size Requirement Estimator”</a> for
        more information.
</p>
</div>

<div class="simplesect">

<div class="titlepage">
<div>

<div class="simple">
<h3 class="title"><a name="data-types-storage-reqs-numeric"></a>Numeric Type Storage Requirements</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091691090880"></a><a class="indexterm" name="idm140091691089392"></a>
<div class="informaltable">
<table summary="Storage required for numeric data types."><colgroup><col width="40%"><col width="60%"></colgroup><thead><tr>
            <th scope="col">Data Type</th>
            <th scope="col">Storage Required</th>
          </tr></thead><tbody><tr>
            <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">TINYINT</code></a></td>
            <td>1 byte</td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">SMALLINT</code></a></td>
            <td>2 bytes</td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">MEDIUMINT</code></a></td>
            <td>3 bytes</td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT</code></a>,
              <a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INTEGER</code></a></td>
            <td>4 bytes</td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a></td>
            <td>8 bytes</td>
          </tr><tr>
            <td scope="row"><code class="literal">FLOAT(<em class="replaceable"><code>p</code></em>)</code></td>
            <td>4 bytes if 0 &lt;= <em class="replaceable"><code>p</code></em> &lt;= 24, 8 bytes if 25
              &lt;= <em class="replaceable"><code>p</code></em> &lt;= 53</td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a></td>
            <td>4 bytes</td>
          </tr><tr>
            <td scope="row"><code class="literal">DOUBLE [PRECISION]</code>,
              <a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">REAL</code></a></td>
            <td>8 bytes</td>
          </tr><tr>
            <td scope="row"><code class="literal">DECIMAL(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code>,
              <code class="literal">NUMERIC(<em class="replaceable"><code>M</code></em>,<em class="replaceable"><code>D</code></em>)</code></td>
            <td>Varies; see following discussion</td>
          </tr><tr>
            <td scope="row"><code class="literal">BIT(<em class="replaceable"><code>M</code></em>)</code></td>
            <td>approximately (<em class="replaceable"><code>M</code></em>+7)/8 bytes</td>
</tr></tbody></table>
</div>
<p>
        Values for <a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a> (and
        <a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">NUMERIC</code></a>) columns are represented
        using a binary format that packs nine decimal (base 10) digits
        into four bytes. Storage for the integer and fractional parts of
        each value are determined separately. Each multiple of nine
        digits requires four bytes, and the <span class="quote">“<span class="quote">leftover</span>”</span>
        digits require some fraction of four bytes. The storage required
        for excess digits is given by the following table.
</p>
<div class="informaltable">
<table summary="Storage required by excess/leftover digits in DECIMAL values."><colgroup><col width="25%"><col width="25%"></colgroup><thead><tr>
            <th scope="col">Leftover Digits</th>
            <th scope="col">Number of Bytes</th>
          </tr></thead><tbody><tr>
            <td scope="row">0</td>
            <td>0</td>
          </tr><tr>
            <td scope="row">1</td>
            <td>1</td>
          </tr><tr>
            <td scope="row">2</td>
            <td>1</td>
          </tr><tr>
            <td scope="row">3</td>
            <td>2</td>
          </tr><tr>
            <td scope="row">4</td>
            <td>2</td>
          </tr><tr>
            <td scope="row">5</td>
            <td>3</td>
          </tr><tr>
            <td scope="row">6</td>
            <td>3</td>
          </tr><tr>
            <td scope="row">7</td>
            <td>4</td>
          </tr><tr>
            <td scope="row">8</td>
            <td>4</td>
</tr></tbody></table>
</div>

</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="data-types-storage-reqs-date-time"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">日期和时间类型存储要求</font></font></h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091691012512"></a><a class="indexterm" name="idm140091691011024"></a><a class="indexterm" name="idm140091691009536"></a><a class="indexterm" name="idm140091691008048"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        对于</font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列，在MySQL 5.6.4之前创建的表所需的存储与从5.6.4创建的表不同。</font><font style="vertical-align: inherit;">这是由于5.6.4中的更改允许这些类型具有小数部分，这需要0到3个字节。
</font></font></p>
<div class="informaltable">
<table summary="Storage required for date and time data types before MySQL 5.6.4 and as of MySQL 5.6.4."><colgroup><col width="20%"><col width="40%"><col width="40%"></colgroup><thead><tr>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型</font></font></th>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL 5.6.4之前需要存储</font></font></th>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">从MySQL 5.6.4开始需要存储</font></font></th>
          </tr></thead><tbody><tr>
            <td scope="row"><a class="link" href="data-types.html#year" title="11.3.3年份类型"><code class="literal">YEAR</code></a></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">1个字节</font></font></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">1个字节</font></font></td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATE</code></a></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">3个字节</font></font></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">3个字节</font></font></td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">3个字节</font></font></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">3字节+小数秒存储</font></font></td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">8个字节</font></font></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">5字节+小数秒存储</font></font></td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">4字节</font></font></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">4字节+小数秒存储</font></font></td>
</tr></tbody></table>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在MySQL 5.6.4，储存
         </font></font><a class="link" href="data-types.html#year" title="11.3.3年份类型"><code class="literal">YEAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">保持不变。</font><font style="vertical-align: inherit;">然而
         </font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">TIMESTAMP</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以不同方式表示。</font></font><a class="link" href="data-types.html#datetime" title="11.3.1 DATE，DATETIME和TIMESTAMP类型"><code class="literal">DATETIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">更有效地打包，非分数部分需要5个而不是8个字节，并且所有三个部分都有一个小数部分，需要0到3个字节，具体取决于存储值的小数秒精度。
</font></font></p>
<div class="informaltable">
<table summary="Required storage for fractional seconds precision."><colgroup><col width="50%"><col width="50%"></colgroup><thead><tr>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">分数秒精度</font></font></th>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">需要存储</font></font></th>
          </tr></thead><tbody><tr>
            <td scope="row"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">0</font></font></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">0个字节</font></font></td>
          </tr><tr>
            <td scope="row"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">1,2</font></font></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">1个字节</font></font></td>
          </tr><tr>
            <td scope="row"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">3,4</font></font></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">2个字节</font></font></td>
          </tr><tr>
            <td scope="row"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">5,6</font></font></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">3个字节</font></font></td>
</tr></tbody></table>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        例如，</font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME(0)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME(2)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME(4)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，和
         </font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME(6)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">分别用3，4，5，和6个字节。</font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">并且
         </font></font><a class="link" href="data-types.html#time" title="11.3.2 TIME类型"><code class="literal">TIME(0)</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">是等效的并且需要相同的存储空间。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        有关时间值的内部表示的详细信息，请参阅</font></font><a class="ulink" href="https://dev.mysql.com/doc/internals/en/algorithms.html" target="_top"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL内部：重要算法和结构</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
</font></font></p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="data-types-storage-reqs-strings"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符串类型存储要求</font></font></h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091690939696"></a><a class="indexterm" name="idm140091690938208"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        在下表中，</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表示非二进制字符串类型的声明列长度（以字符为单位）和二进制字符串类型的字节。
        </font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">表示给定字符串值的实际长度（以字节为单位）。
</font></font></p>
<div class="informaltable">
<table summary="Storage required for string types."><colgroup><col width="40%"><col width="60%"></colgroup><thead><tr>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数据类型</font></font></th>
            <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">需要存储</font></font></th>
          </tr></thead><tbody><tr>
            <td scope="row"><code class="literal">CHAR(<em class="replaceable"><code>M</code></em>)</code></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">紧凑的InnoDB行格式系列优化了可变长度字符集的存储。</font><font style="vertical-align: inherit;">请参阅
               </font></font><a class="xref" href="innodb-storage-engine.html#innodb-compact-row-format-characteristics" title="紧凑行格式存储特性"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">COMPACT行格式存储特性</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">否则，</font></font><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">×
               </font></font><em class="replaceable"><code>w</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">bytes，</font><font style="vertical-align: inherit;">255，其中
               </font><font style="vertical-align: inherit;">是字符集中最大长度字符所需的字节数。</font></font><code class="literal">&lt;=
              <em class="replaceable"><code>M</code></em> &lt;=</code><font style="vertical-align: inherit;"></font><em class="replaceable"><code>w</code></em><font style="vertical-align: inherit;"></font></td>
          </tr><tr>
            <td scope="row"><code class="literal">BINARY(<em class="replaceable"><code>M</code></em>)</code></td>
            <td><em class="replaceable"><code>M</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字节，0 </font><font style="vertical-align: inherit;">255</font></font><code class="literal">&lt;=
              <em class="replaceable"><code>M</code></em> &lt;=</code><font style="vertical-align: inherit;"></font></td>
          </tr><tr>
            <td scope="row"><code class="literal">VARCHAR(<em class="replaceable"><code>M</code></em>)</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
              </font></font><code class="literal">VARBINARY(<em class="replaceable"><code>M</code></em>)</code></td>
            <td><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">如果列值需要0  -  255个字节，</font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">则为+ 1个字节;如果值可能需要超过255个字节，则为+ 2个字节</font></font></td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TINYBLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
              </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TINYTEXT</code></a></td>
            <td><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">+ 1个字节，其中
               </font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">&lt;2 </font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">8</font></font></sup></td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">， </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a></td>
            <td><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">+ 2个字节，其中
               </font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">&lt;2 </font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">16</font></font></sup></td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMBLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
              </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMTEXT</code></a></td>
            <td><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">+ 3个字节，其中
               </font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">&lt;2 </font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">24</font></font></sup></td>
          </tr><tr>
            <td scope="row"><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGBLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
              </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">LONGTEXT</code></a></td>
            <td><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">+ 4个字节，其中
               </font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">&lt;2 </font></font><sup><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">32</font></font></sup></td>
          </tr><tr>
            <td scope="row"><code class="literal">ENUM('<em class="replaceable"><code>value1</code></em>','<em class="replaceable"><code>value2</code></em>',...)</code></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">1或2个字节，具体取决于枚举值的数量（最多65,535个值）</font></font></td>
          </tr><tr>
            <td scope="row"><code class="literal">SET('<em class="replaceable"><code>value1</code></em>','<em class="replaceable"><code>value2</code></em>',...)</code></td>
            <td><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">1个，2个，3个，4个或8个字节，具体取决于设置成员的数量（最多64个成员）</font></font></td>
</tr></tbody></table>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        使用长度前缀加数据存储可变长度字符串类型。</font><font style="vertical-align: inherit;">长度前缀需要1到4个字节，具体取决于数据类型，前缀的值是
         </font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">（字符串的字节长度）。</font><font style="vertical-align: inherit;">例如，</font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMTEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">值的</font><font style="vertical-align: inherit;">存储
         </font><font style="vertical-align: inherit;">需要
         </font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字节来存储值加上三个字节来存储值的长度。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        为了计算用于存储特定的字节数
         </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">CHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
         </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列的值时，必须考虑到用于该列是否值包含多字节字符的字符集。</font><font style="vertical-align: inherit;">特别是，在使用</font></font><code class="literal">utf8</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Unicode字符集时，必须记住并非所有字符都使用相同的字节数。</font></font><code class="literal">utf8mb3</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和</font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        字符集每个字符最多可分别需要三个和四个字节。</font><font style="vertical-align: inherit;">有关用于不同类别</font></font><code class="literal">utf8mb3</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">或
         </font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符</font><font style="vertical-align: inherit;">的存储的细分</font><font style="vertical-align: inherit;">，请参见
         </font></font><a class="xref" href="charset.html#charset-unicode" title="10.9 Unicode支持"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第10.9节“Unicode支持”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><a class="indexterm" name="idm140091690871760"></a><a class="indexterm" name="idm140091690870272"></a><a class="indexterm" name="idm140091690868784"></a><p>
        <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
         </font></font><a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">VARBINARY</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">以及
         </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">和
         </font></font><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型是可变长度类型的。</font><font style="vertical-align: inherit;">对于每个，存储要求取决于以下因素：
</font></font></p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            列值的实际长度
          </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            列的最大可能长度
          </font></font></p></li><li class="listitem"><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
            用于该列的字符集，因为某些字符集包含多字节字符
</font></font></p></li></ul>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
        例如，</font></font><code class="literal">VARCHAR(255)</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">列可以包含最大长度为255个字符的字符串。</font><font style="vertical-align: inherit;">假设列使用</font></font><code class="literal">latin1</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字符集（每个字符一个字节），所需的实际存储量是字符串的长度（</font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）</font><font style="vertical-align: inherit;">，加上一个字节来记录字符串的长度。</font><font style="vertical-align: inherit;">对于字符串
         </font></font><code class="literal">'abcd'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，</font></font><em class="replaceable"><code>L</code></em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">为4，存储要求为5个字节。</font><font style="vertical-align: inherit;">如果声明同一列使用</font></font><code class="literal">ucs2</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">双字节字符集，则存储要求为10个字节：长度</font></font><code class="literal">'abcd'</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">为8个字节，并且该列需要两个字节来存储长度，因为最大长度大于255（最多510个）字节）。
      </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">可以存储在一个</font><font style="vertical-align: inherit;">或一
         </font><font style="vertical-align: inherit;">列中</font><font style="vertical-align: inherit;">
        的有效最大</font></font><span class="emphasis"><em><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">字节</font></font></em></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">数</font><font style="vertical-align: inherit;">受最大行大小65,535字节的影响，该字节在所有列之间共享。</font><font style="vertical-align: inherit;">对于</font><font style="vertical-align: inherit;">存储多字节字符</font><font style="vertical-align: inherit;">的</font><font style="vertical-align: inherit;">列，有效最大</font><span class="emphasis"><em><font style="vertical-align: inherit;">字符</font></em></span><font style="vertical-align: inherit;">数较少。</font><font style="vertical-align: inherit;">例如，
         </font><font style="vertical-align: inherit;">字符每个字符最多需要四个字节，因此</font><font style="vertical-align: inherit;">
        使用该</font><font style="vertical-align: inherit;">字符集</font><font style="vertical-align: inherit;">的</font><font style="vertical-align: inherit;">列</font><font style="vertical-align: inherit;">可以声明为最多16,383个字符。</font><font style="vertical-align: inherit;">请参见
         </font><a class="xref" href="restrictions.html#column-count-limit" title="C.10.4表列数和行大小的限制"><font style="vertical-align: inherit;">第C.10.4节“表列数和行大小的限制”</font></a><font style="vertical-align: inherit;">。
      </font></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#binary-varbinary" title="11.4.2 BINARY和VARBINARY类型"><code class="literal">VARBINARY</code></a><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"></font><span class="emphasis"><em><font style="vertical-align: inherit;"></font></em></span><font style="vertical-align: inherit;"></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"></font><a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a><font style="vertical-align: inherit;"></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"></font><a class="xref" href="restrictions.html#column-count-limit" title="C.10.4表列数和行大小的限制"><font style="vertical-align: inherit;"></font></a><font style="vertical-align: inherit;"></font></p><p>
        <code class="literal">InnoDB</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">将长度大于或等于768字节的固定长度字段编码为可变长度字段，可以在页外存储。</font><font style="vertical-align: inherit;">例如，
         </font></font><code class="literal">CHAR(255)</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">如果字符集的最大字节长度大于3，则列可以超过768字节</font></font><code class="literal">utf8mb4</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
      </font></font></p><p>
        The <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a> storage engine supports
        variable-width columns. This means that a
        <a class="link" href="data-types.html#char" title="11.4.1 CHAR和VARCHAR类型"><code class="literal">VARCHAR</code></a> column in an NDB Cluster
        table requires the same amount of storage as would any other
        storage engine, with the exception that such values are 4-byte
        aligned. Thus, the string <code class="literal">'abcd'</code> stored in a
        <code class="literal">VARCHAR(50)</code> column using the
        <code class="literal">latin1</code> character set requires 8 bytes (rather
        than 5 bytes for the same column value in a
        <code class="literal">MyISAM</code> table).
      </p><p>
        <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">TEXT</code></a> and
        <a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">BLOB</code></a> columns are implemented
        differently in <a class="link" href="mysql-cluster.html" title="第22章MySQL NDB Cluster 8.0"><code class="literal">NDB</code></a>; each row in a
        <code class="literal">TEXT</code> column is made up of two separate parts.
        One of these is of fixed size (256 bytes), and is actually
        stored in the original table. The other consists of any data in
        excess of 256 bytes, which is stored in a hidden table. The rows
        in this second table are always 2000 bytes long. This means that
        the size of a <code class="literal">TEXT</code> column is 256 if
        <em class="replaceable"><code>size</code></em> &lt;= 256 (where
        <em class="replaceable"><code>size</code></em> represents the size of the row);
        otherwise, the size is 256 +
        <em class="replaceable"><code>size</code></em> + (2000 ×
        (<em class="replaceable"><code>size</code></em> − 256) % 2000).
      </p><a class="indexterm" name="idm140091690828080"></a><p>
        The size of an <a class="link" href="data-types.html#enum" title="11.4.4 ENUM类型"><code class="literal">ENUM</code></a> object is
        determined by the number of different enumeration values. One
        byte is used for enumerations with up to 255 possible values.
        Two bytes are used for enumerations having between 256 and
        65,535 possible values. See <a class="xref" href="data-types.html#enum" title="11.4.4 ENUM类型">Section&nbsp;11.4.4, “The ENUM Type”</a>.
      </p><a class="indexterm" name="idm140091690824016"></a><p>
        The size of a <a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET</code></a> object is
        determined by the number of different set members. If the set
        size is <em class="replaceable"><code>N</code></em>, the object occupies
        <code class="literal">(<em class="replaceable"><code>N</code></em>+7)/8</code> bytes,
        rounded up to 1, 2, 3, 4, or 8 bytes. A
        <a class="link" href="data-types.html#set" title="11.4.5 SET类型"><code class="literal">SET</code></a> can have a maximum of 64
        members. See <a class="xref" href="data-types.html#set" title="11.4.5 SET类型">Section&nbsp;11.4.5, “The SET Type”</a>.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="data-types-storage-reqs-gis"></a>Spatial Type Storage Requirements</h3>

</div>

</div>

</div>
<a class="indexterm" name="idm140091690816256"></a><a class="indexterm" name="idm140091690814768"></a><a class="indexterm" name="idm140091690813280"></a><p>
        MySQL stores geometry values using 4 bytes to indicate the SRID
        followed by the WKB representation of the value. The
        <a class="link" href="functions.html#function_length"><code class="literal">LENGTH()</code></a> function returns the
        space in bytes required for value storage.
      </p><p>
        For descriptions of WKB and internal storage formats for spatial
        values, see <a class="xref" href="data-types.html#gis-data-formats" title="11.5.3支持的空间数据格式">Section&nbsp;11.5.3, “Supported Spatial Data Formats”</a>.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="data-types-storage-reqs-json"></a>JSON Storage Requirements</h3>

</div>

</div>

</div>
<p>
        In general, the storage requirement for a
        <a class="link" href="data-types.html#json" title="11.6 JSON数据类型"><code class="literal">JSON</code></a> column is approximately the
        same as for a <code class="literal">LONGBLOB</code> or
        <code class="literal">LONGTEXT</code> column; that is, the space consumed
        by a JSON document is roughly the same as it would be for the
        document's string representation stored in a column of one
        of these types. However, there is an overhead imposed by the
        binary encoding, including metadata and dictionaries needed for
        lookup, of the individual values stored in the JSON document.
        For example, a string stored in a JSON document requires 4 to 10
        bytes additional storage, depending on the length of the string
        and the size of the object or array in which it is stored.
      </p><p>
        In addition, MySQL imposes a limit on the size of any JSON
        document stored in a <code class="literal">JSON</code> column such that it
        cannot be any larger than the value of
        <a class="link" href="server-administration.html#sysvar_max_allowed_packet"><code class="literal">max_allowed_packet</code></a>.
</p>
</div>

</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="choosing-types"></a>11.9&nbsp;Choosing the Right Type for a Column</h2>

</div>

</div>

</div>
<a class="indexterm" name="idm140091690799744"></a><a class="indexterm" name="idm140091690798288"></a><p>
      For optimum storage, you should try to use the most precise type
      in all cases. For example, if an integer column is used for values
      in the range from <code class="literal">1</code> to
      <code class="literal">99999</code>, <code class="literal">MEDIUMINT UNSIGNED</code> is
      the best type. Of the types that represent all the required
      values, this type uses the least amount of storage.
    </p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      所有基本计算（</font></font><code class="literal">+</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，
       </font></font><code class="literal">-</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，</font></font><code class="literal">*</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">，和
       </font></font><code class="literal">/</code><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">）与</font></font><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      列与65（十进制）（基体10）位的精度进行。</font><font style="vertical-align: inherit;">请参见</font></font><a class="xref" href="data-types.html#numeric-type-overview" title="11.1.1数字类型概述"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第11.1.1节“数字类型概述”</font></font></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。
    </font></font></p><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      如果准确性不是太重要或者速度是最高优先级，那么</font></font><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">类型可能足够好。</font><font style="vertical-align: inherit;">对于高精度，您始终可以转换为存储在a中的定点类型
       </font></font><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">。</font><font style="vertical-align: inherit;">这使您可以使用64位整数进行所有计算，然后根据需要将结果转换回浮点值。
</font></font></p>
</div>
<div class="section">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a name="other-vendor-data-types"></a><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">11.10使用其他数据库引擎中的数据类型</font></font></h2>

</div>

</div>

</div>
<a class="indexterm" name="idm140091690784240"></a><a class="indexterm" name="idm140091690782784"></a><a class="indexterm" name="idm140091690781296"></a><a class="indexterm" name="idm140091690779808"></a><a class="indexterm" name="idm140091690778320"></a><p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      为了便于使用为其他供应商的SQL实现编写的代码，MySQL映射数据类型，如下表所示。</font><font style="vertical-align: inherit;">这些映射使得将表定义从其他数据库系统导入MySQL变得更加容易。
</font></font></p>
<div class="informaltable">
<table summary="Mapping of MySQL data types to data types from other vendors."><colgroup><col width="35%"><col width="55%"></colgroup><thead><tr>
          <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">其他供应商类型</font></font></th>
          <th scope="col"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL类型</font></font></th>
        </tr></thead><tbody><tr>
          <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BOOL</code></a></td>
          <td><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">TINYINT</code></a></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BOOLEAN</code></a></td>
          <td><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">TINYINT</code></a></td>
        </tr><tr>
          <td scope="row"><code class="literal">CHARACTER VARYING(<em class="replaceable"><code>M</code></em>)</code></td>
          <td><code class="literal">VARCHAR(<em class="replaceable"><code>M</code></em>)</code></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">FIXED</code></a></td>
          <td><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT4</code></a></td>
          <td><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT</code></a></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">FLOAT8</code></a></td>
          <td><a class="link" href="data-types.html#floating-point-types" title="11.2.3浮点类型（近似值） -  FLOAT，DOUBLE"><code class="literal">DOUBLE</code></a></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT1</code></a></td>
          <td><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">TINYINT</code></a></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT2</code></a></td>
          <td><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">SMALLINT</code></a></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT3</code></a></td>
          <td><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">MEDIUMINT</code></a></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT4</code></a></td>
          <td><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">INT</code></a></td>
        </tr><tr>
          <td scope="row"><code class="literal">INT8</code></td>
          <td><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">BIGINT</code></a></td>
        </tr><tr>
          <td scope="row"><code class="literal">LONG VARBINARY</code></td>
          <td><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMBLOB</code></a></td>
        </tr><tr>
          <td scope="row"><code class="literal">LONG VARCHAR</code></td>
          <td><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMTEXT</code></a></td>
        </tr><tr>
          <td scope="row"><code class="literal">LONG</code></td>
          <td><a class="link" href="data-types.html#blob" title="11.4.3 BLOB和TEXT类型"><code class="literal">MEDIUMTEXT</code></a></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">MIDDLEINT</code></a></td>
          <td><a class="link" href="data-types.html#integer-types" title="11.2.1整数类型（精确值） -  INTEGER，INT，SMALLINT，TINYINT，MEDIUMINT，BIGINT"><code class="literal">MEDIUMINT</code></a></td>
        </tr><tr>
          <td scope="row"><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">NUMERIC</code></a></td>
          <td><a class="link" href="data-types.html#fixed-point-types" title="11.2.2定点类型（精确值） -  DECIMAL，NUMERIC"><code class="literal">DECIMAL</code></a></td>
</tr></tbody></table>
</div>
<p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
      数据类型映射在表创建时发生，之后将丢弃原始类型规范。</font><font style="vertical-align: inherit;">如果您创建一个包含其他供应商使用的类型的表，然后发出一个
       </font><font style="vertical-align: inherit;">
      语句，MySQL会使用等效的MySQL类型报告表结构。</font><font style="vertical-align: inherit;">例如：
    </font></font><code class="literal">DESCRIBE <em class="replaceable"><code>tbl_name</code></em></code><font style="vertical-align: inherit;"></font></p><pre data-lang="sql" class="programlisting"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">MySQL的&gt; </font></font><strong class="userinput"><code>CREATE TABLE t (a BOOL, b FLOAT8, c LONG VARCHAR, d NUMERIC);</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
查询正常，0行受影响（0.00秒）</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
MySQL的&gt; </font></font><strong class="userinput"><code>DESCRIBE t;</code></strong><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------- + -------- + ------ + ------ + --------- +  - ----- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">领域| </font><font style="vertical-align: inherit;">输入| </font><font style="vertical-align: inherit;">空| </font><font style="vertical-align: inherit;">钥匙| </font><font style="vertical-align: inherit;">默认| </font><font style="vertical-align: inherit;">额外的|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------- + -------- + ------ + ------ + --------- +  - ----- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">a | </font><font style="vertical-align: inherit;">tinyint（1）| </font><font style="vertical-align: inherit;">是的| </font><font style="vertical-align: inherit;">| </font><font style="vertical-align: inherit;">NULL | </font><font style="vertical-align: inherit;">|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">b | </font><font style="vertical-align: inherit;">双| </font><font style="vertical-align: inherit;">是的| </font><font style="vertical-align: inherit;">| </font><font style="vertical-align: inherit;">NULL | </font><font style="vertical-align: inherit;">|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">c | </font><font style="vertical-align: inherit;">中文| </font><font style="vertical-align: inherit;">是的| </font><font style="vertical-align: inherit;">| </font><font style="vertical-align: inherit;">NULL | </font><font style="vertical-align: inherit;">|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
| </font><font style="vertical-align: inherit;">d | </font><font style="vertical-align: inherit;">十进制（10,0）| </font><font style="vertical-align: inherit;">是的| </font><font style="vertical-align: inherit;">| </font><font style="vertical-align: inherit;">NULL | </font><font style="vertical-align: inherit;">|</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
+ ------- + -------- + ------ + ------ + --------- +  - ----- +</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
4行（0.01秒）</font></font><font></font>
</pre>
</div>

</div>
<div class="copyright-footer">

</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tbody><tr>
<td width="40%" align="left"><a accesskey="p" href="charset.html"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">上一页</font></font></a></td>
<td width="20%" align="center"><a accesskey="u" href=""><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">向上</font></font></a></td>
<td width="40%" align="right">&nbsp;<a accesskey="n" href="functions.html"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">下一个</font></font></a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第10章字符集，排序规则，Unicode</font></font></td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">家</font></font></a></td>
<td width="40%" align="right" valign="top"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">第12章函数和操作符</font></font></td>
</tr>
</tbody></table>
</div>


<div id="goog-gt-tt" class="skiptranslate" dir="ltr"><div style="padding: 8px;"><div><div class="logo"><img src="https://www.gstatic.com/images/branding/product/1x/translate_24dp.png" width="20" height="20" alt="Google 翻译"></div></div></div><div class="top" style="padding: 8px; float: left; width: 100%;"><h1 class="title gray">原文</h1></div><div class="middle" style="padding: 8px;"><div class="original-text"></div></div><div class="bottom" style="padding: 8px;"><div class="activity-links"><span class="activity-link">提供更好的翻译建议</span><span class="activity-link"></span></div><div class="started-activity-container"><hr style="color: #CCC; background-color: #CCC; height: 1px; border: none;"><div class="activity-root"></div></div></div><div class="status-message" style="display: none;"></div></div><div id="cntvlive2-is-installed"></div><div class="goog-te-spinner-pos"><div class="goog-te-spinner-animation"><svg xmlns="http://www.w3.org/2000/svg" class="goog-te-spinner" width="96px" height="96px" viewBox="0 0 66 66"><circle class="goog-te-spinner-path" fill="none" stroke-width="6" stroke-linecap="round" cx="33" cy="33" r="30"></circle></svg></div></div></body></html>